passing const ptr argument

From: Anthony (anthony_at_notspecified.nl)
Date: 08/11/04


Date: Wed, 11 Aug 2004 10:06:54 +0200

Hi,
I want to write protect an argument that is passed by reference.
But I still want to be able to call members of that argument.
E.g. code (bottum) will gives the following compiler error:
writeProtEvt.cpp: In member function `virtual void A::print(const B*)':
writeProtEvt.cpp:24: error: passing `const B' as `this' argument of `virtual
   void B::print()' discards qualifiers

Can anyone explain how to do this the right way?

thanks in advance,
Anthony Lansbergen

// -------------------------------------------------------------------------

---
#include <ostream.h>
Class B
{
    public:
        virtual void print()
        {
            cout << "printB" << std::endl;
        }
};
class A
{
    public:
        virtual void print(const B* b)
        {
            cout << "printA" << std::endl;
            b->print();
        }
};
main()
{
    cout << "test" << std::endl;
    A* a;
    B* b;
    a = new A();
    b = new B();
    a->print(b);
}


Relevant Pages

  • Re: One time code
    ... my first reaction to this was to think of having a single serial port control thread ... virtual void Initialize() PURE; ... it's the kind of code I might type in if I needed a singleton class that was reference ...
    (microsoft.public.vc.mfc)
  • Re: why does this not compile ?
    ... Artie Gold wrote: ... >> virtual void a{ ... >> int main{ ... > reference to object, ...
    (comp.lang.cpp)