friends, private, break of encapsulation?

M

Marc Schellens

If I have a base class with a public virtual member function,
a child class will grand access to its overridden memeber function
even if its private in the child class, as the compliler cannot
know at compile time that the overridden function is called, right?
Doesn't that break encapsulation?
Same with friends of the base class.
Does it make sense to declare the method in the derived class as private
then at all?
A programmer only looking at the derived class might get a wrong
impression...
marc
 
V

Victor Bazarov

Marc Schellens said:
If I have a base class with a public virtual member function,
a child class will grand access to its overridden memeber function
even if its private in the child class, as the compliler cannot
know at compile time that the overridden function is called, right?

No. Only if accessed through a pointer to the base will the
function be executed. And the base class has explicitly given
access to anybody.
Doesn't that break encapsulation?

No, it does not.
Same with friends of the base class.

I am not sure I understand this statement. Friendship is not
inherited.
Does it make sense to declare the method in the derived class as private
then at all?

Depends on the situation.
A programmer only looking at the derived class might get a wrong
impression...

Please give an example of "getting wrong impression". The usual
reason to make final overriders private is to prevent them from
being used in non-polymorphic way.

Victor
 
V

Victor Bazarov

Marc Schellens said:
But thats not polymorphism, is it?
Or what would be the difference here without 'virtual'?

No difference, I suppose. Try making 'D::foo' private.
d.foo() will cause errors.

This example was meant to illustrate calling the function
directly and specifying the class in the call.

Victor
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top