Reputation: 4106
I do not understand the following restriction:
class Base
{
virtual doFoo() = 0;
};
class Derived : public Base
{
public:
void doStuff() { doFoo(); } //compile error, doFoo is a private member of Base
};
The solution is of course to re-declare the member in the derived class but it seems somewhat odd to me:
class Derived : public Base
{
public:
void doStuff() { doFoo(); }
private:
virtual doFoo() = 0;
};
Why cannot I use the base class' abstract virtual private method in the derived class without re-declaring it? It seems to be overly rigid restriction because if I used it but not defined it then Derived
is still abstract and to instantiate it some further derived class would have to provide doFoo
.
Intuitively I would say that by not defining it (or re-declaring it) I imply that method is not used and is not part of the derived class but that is more for the reader of the code than for the compiler. Or maybe the compiler does need that information as well and that is the real reason for this?
Upvotes: 3
Views: 1542
Reputation: 688
If you want to access this method from a derived class, a good approach is to declare it protected
.
Here, even if you don't declare the method as private
, the default access of class
is private
. Which means that you are implicitly declaring this method as private
.
(Had you been using struct
instead, the default access would have been public
.)
Upvotes: 0
Reputation: 138
Private base class functions can not be called directly within derived class methods. However this inaccessibility by the derived class does not have anything to do with the virtual call mechanism, which is to the derived class.
There are cases where this is desirable. Taken from the C++ FAQ.
You might ask, What good is a method that the derived class can’t call? Even though the derived class can’t call it in the base class, the base class can call it which effectively calls down to the (appropriate) derived class. And that’s what the Template Method pattern is all about.
Think of “Back to the Future.” Assume the base class is written last year, and you are about to create a new derived class later today. The base class’ methods, which might have been compiled and stuck into a library months ago, will call the private (or protected) virtual, and that will effectively “call into the future” - the code which was compiled months ago will call code that doesn’t even exist yet - code you are about to write in the next few minutes. You can’t access private members of the base class - you can’t reach into the past, but the past can reach into the future and call your methods which you haven’t even written yet.
Upvotes: 5
Reputation: 76245
Declaring a new member function in the derived class does not give you access to the one with the same name in the base class. The one in the base class is still private, and you can't access it from outside the base class (absent friend declarations). This has nothing to do with the member function being pure virtual; it applies to all names. If you don't want it to be private, don't define it as private.
Upvotes: 3