A C++ question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

It is a C++ question and may not be applicable to this list. But, still if somebody can explain the behaviour. I have a small code as below:

class B
{
   public:
       int b;
       B () { b  = 0; };
       void foo2 (int h) { b = h; }
};

class A
{
   public:
       int a;
       A () { cout << "Inside default constructor" << endl; b = new B ();}

       B* b;

void bar () const { b->foo2 (3); } // problem. how can this compile ?
};


void foo (const A* a)
{
   a->bar ();
}

int
main (int argc, char** argv)
{
   A a1;
   foo (&a1);
}

My concern is how could this code compile when in bar () function of class A, I am calling non const function on pointer b ?

If instead of pointer, b is an object of type B, I get correct error saying that inside const member function of class A, I cannot call non const member function of B.

Thanks in advance,

-Aseem.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux