accessing base class public member from derived class

c++, inheritance

Solution

You inherit privately from the base class. What you typically need is public inheritance:

class derived : public base

Here is the FAQ on private inheritance.

Problem

Is it possible to access base class public member from instance of derived class in some other locations in the program. ``` class base { public: int x; base(int xx){ x = xx; } }; class derived : base { public: derived(int xx) : base(xx){ } }; class main { public: derived * myDerived; void m1(){ myDerived = new derived(5); m2(myDerived); } void m2(derived * myDerived){ printf("%i", myDerived->x); } }; ``` After above code, I got following error. ``` `error: 'int base::x' is inaccessible` ```

Original source