Given the following classes:
class foo
{
private:
int c;
public:
foo( int a = 42 ) { c = a; }
~foo();
};
class bar: public foo
{
public:
bar();
~bar();
};
How can I make bar override c with a different number? Can I do something like this?
bar::bar()
{
c = 12;
}
I get this error when trying to compile:
test.cpp: In constructor
‘bar::bar()’:
test.cpp:8:7: error:‘int foo::c’is private
Call your base class’ constructor in the constructor initialization list:
Incidentally, you should always prefer using a constructor initialization list over assignment inside the constructor body, so your
fooconstructor would be better written as: