The reason you cannot access them from D
is because they are private
, which means they are only accessible from within D
itself. In order to be able to access them only from D
or its subclasses, you need to use the protected
access modifier instead:
class C
{
private:
int x;
int y;
int energy;
protected:
A &a;
B &b;
public:
C(A &a, B &b);
void print(void);
virtual void printAt(void);
/// ...
};
Now, a bit of terminology:
When you type class C : public D
you are not making it a friend
, you are inheriting from it. This means C
will be a base class of D
. A friend is another, related concept.
A friend of some class is another class which has access to its private
properties. So, if you instead had made D
a friend
of C
, you would have had access to a
and b
without having to make them protected
. This would be accomplished as such:
class C
{
// Some code...
friend D;
//Lots of code ...
}
Please note that, for this to work, you need to declare D
before C
.
Now, which of these options should you use?
Ask yourself this question: is a D
logically a more specific type of C
? If so, it should use inheritance. If not, it may be better to make D
have a member of type C
and use the friend
keyword. In either case, use friend
sparingly, and only if there is necessarily a very tight relationship between the two classes (perhaps if D
is a factory for type C
and C
has a private constructor.)