Can a sub-class (composite object) member function access private or protected members of base class

P

pkpatil

Hi,

Can a private composite object in a class access private or protected
members of base class?
For e.g.

class composite {
void memberFunction();
};

class main {
private:
int a;
composite comp;
protected:
int b;
};

Above, can composite::memberFunction() access main::a and main::b of
main class "main"? If so, how?
 
V

Victor Bazarov

Can a private composite object in a class access private or protected
members of base class?
For e.g.

class composite {
void memberFunction();
};

class main {
private:
int a;
composite comp;
protected:
int b;
};

Above, can composite::memberFunction() access main::a and main::b of
main class "main"? If so, how?

No, it cannot. The relationship exists between types and functions, not
between objects and classes or between objects and objects. In your case,
the 'composite::memberFunction' has no special access rights to class
'main'. To give the right to that function you need to declare it 'friend'
to 'main' class.

V
 
N

Noah Roberts

Hi,

Can a private composite object in a class access private or protected
members of base class?

There is no 'base class' in your example. Base classes are the class
that a derived class derives from. It has to do with inheritance, not
/composition/ as you show below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top