1

i have this :

class A {
    public :
        A(int i ) : m_S(i)
        {  
            m_Pa = new Foo(*this) ;
        }
    private :
        int m_S ;
        Foo* m_Pa;
}

and derived class 

class B : public A {
    public :
        B() : A (242) 
        {
          // here i like to override the A class m_Pa member but i don't know how to do it right 
        }
}
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
user63898
  • 29,839
  • 85
  • 272
  • 514

4 Answers4

1

What is m_Pa? You never had it declared. Assuming it's a private data member of type Foo* in class A, you can't directly change it in the derived class unless you change A's interface. E.g., you can provide a protected setter member function:

class A {
....
protected:
 void setFoo(const Foo* foo);
}

class B {
 ....
 Foo *foo = new Foo(this);
 setFoo(foo);
}
igor
  • 419
  • 1
  • 4
  • 6
1

your m_Pa should be protected than you can call like:

 B() : A (242), m_Pa(12) 
 {

 }

or

 B() : A (242)
 {
   m_PA = 55
 }

or you should make a public or protected function which changes m_Pa

 class A {
     public :
         A(int i ) : m_S(i)
         {  
              m_Pa = new Foo(*this) ;
         }

        void setPA(int val)
        {
             m_PA = val;
        }
ufukgun
  • 6,889
  • 8
  • 33
  • 55
  • when i try your first example im getting B : illegal member initialization: 'm_PA' is not a base or member – user63898 Jul 14 '09 at 08:35
  • that is why you wrote m_Pa under private. you can not change a private member if it is not base class. you can only change protected or public members from a derived class. also probably you wrote ":" instead of "," before m_Pa(new Foo(*this)) – ufukgun Jul 14 '09 at 08:54
0

You can't override member variables in a derived class, only methods.

DaveR
  • 9,540
  • 3
  • 39
  • 58
0

Short answer: By declaring m_Pa private, you are saying that only class A should be able to modify it. So you cannot alter its value using methods of class B.

Longer answer: In C++ (and most other object oriented programming languages), you not only declare the type of a member, but also its visibility (public, protected and private). This allows you to enfore encapsulation of data: You only expose an interface, but you do not let clients of your class modify its internals directly.

In your concrete example, I would create accessors

Foo* getPa() {
  return m_Pa;
}

void setPa(Foo* Pa) {
  m_Pa = Pa;
}

in class A and use them in class B to modify m_Pa. If you would like class B (but not unrelated classes) to be able to modify m_Pa declare getPa() and setPa() in a protected: section of your class; if you would like any client to modify them declare them in a public: section. Especially in the later case you need to start worrying about object ownership, i.e. which object is responsible for deleting the object stored in m_Pa which is created in the constructor. A practical solution to this problem is to use smart pointers, see for instance boost's implementation.

A note on terminology: "Overriding" a member in C++ usually refers to giving a new implementation of a virtual member function. So if your class A has a method

virtual void doIt()

then a member of the same type in class B overrides the implementation of A's doIt().

Tobias
  • 6,388
  • 4
  • 39
  • 64