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`
The problem is that you accidentally use private inheritance here
class derived : base {
This makes all base class members private in the derived class.
Change this to
class derived : public base {
and it will work as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With