Why can't we access a protected member in a derived class by a base class's object?
Code:
#include <iostream> Code:
anisha@linux-dopx:~/> g++ type.cpp |
The interface to X's fun is protected. Therefore you can call it from within a function declared as part of class X or you can call it directly from a class derived from X. What you cant do is have a function of Y and declare a local variable of type X and then call its function. The local copy is not one derived from Y and the function of X is not public to call.
I.e. You can only do: X::Func() { fun() } Y::Func() { fun() } but not: Y:Func() { X x; x.fun() } |
And if you want to call X::fun() from within a function of Y, then something like this could be used:
Code:
class Y : public X |
I think you can also declare Y a "friend" class for X.
Stuck in windows at the moment. Can't send a code example. |
Quote:
|
You can add
Code:
friend class Y; You can also un-protect everything in a class header you #include like this: Code:
#define protected public The above is only for classes you can't easily rewrite or for quick testing your concepts. Another approach would be to make a friend class that doesn't exist so that it can be #defined when the header loads. Something like this. Code:
class X Code:
#define X_accessor Y |
All times are GMT -5. The time now is 04:41 AM. |