How can I possibly do this:
class Base
{
public:
int a, b, c;
void function();
...
};
class Derived1 :
private Base
{
public:
int d, e, f;
...
};
class Derived2 :
private Base
{
public:
void addObject(const Base* obj);
...
};
Note that I inherit it as private
Then I would like to do like:
Base* d1 = new Derived1();
Base* d2 = new Derived2();
d2->addObject(d1);
And here is the relevant issue I am getting from the compiler:
C:\test\test.cpp||In function 'void init()':|
C:\test\test.cpp|423|error: no matching function for call to 'Derived2::addObject(const Base*)'|
C:\test\test.cpp|423|note: candidate is:|
C:\test\test.h|29|note: void Derived2::addObject(const Base*)|
C:\test\test.h|29|note: no known conversion for argument 1 from 'Derived1 {aka ... }' to 'const Base* {aka ... }'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 9 seconds) ===|
The reason I want to inherit the Base
as private by some derived classes is to hide some Base
's members/functions in public scope.
I do not want to directly access some Base::functions()
in a public scope but else, used a Derived::function()
instead to manipulate its data members and let the derived class decide what action it would perform.
However, I could think of overriding each function from base class that I do not want to modify directly in public scope but its not flexible and too many in my case.
What I mean is:
class Base
{
public:
//I must think in advance deciding whether if it'll be virtual or not.
[virtual] void f1(int);
[virtual] void f2(int);
[virtual] void f3(int);
//and many more...
};
class Derivedn :
private Base
{
public:
//hmm, I should not neglect to override certain base' member functions
//that I don't want to access in public scope.
void f1(int);
};
I want to avoid this but...
Is there any other way I can do, similar to this one, without overriding each Base::function()
s?