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 4 2941
"Marc Schellens" <m_*********@hotmail.com> wrote... 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
>>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.
But this kind of access is the point about polymorphism, isn't it?
Maybe I don't see the forest because of too many trees,
but how else could you make use of it? 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.
I meant a friend of the base class can access virtual private methods
of inherted classes through the base class ptr. 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.
I see. But could you give an example when this is wanted?
marc
"Marc Schellens" <m_*********@hotmail.com> wrote... 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.
But this kind of access is the point about polymorphism, isn't it? Maybe I don't see the forest because of too many trees, but how else could you make use of it?
Nothing prevents you from using the function directly. Besides,
you may specify the class explicitly:
struct B {
virtual void foo();
};
struct D : B {
void foo();
};
int main() {
D d;
d.foo(); // will resolve to D::foo no matter what
d.B::foo(); // explicit specification of what to call
} 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.
I meant a friend of the base class can access virtual private methods of inherted classes through the base class ptr.
Substituting the behaviour of the base class with derived's own
is the base (and the purpose) of polymorphism and has no bearing
on privacy and friendship. Even if the base class functions are
virtual and private, I can override those in the derived class
so that when the base class' member accesses those, the member
of the derived class will be called:
struct B {
void foo() { bar(); }
private:
virtual void bar();
};
struct D : B {
private: // doesn't matter at that point
void bar();
};
int main() {
B* pb = new D;
pb->foo(); // will call D::bar
} 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.
I see. But could you give an example when this is wanted?
Not off the top of my head, no.
Victor
"Marc Schellens" <m_*********@hotmail.com> wrote... >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.
But this kind of access is the point about polymorphism, isn't it? Maybe I don't see the forest because of too many trees, but how else could you make use of it?
Nothing prevents you from using the function directly. Besides, you may specify the class explicitly:
struct B { virtual void foo(); };
struct D : B { void foo(); };
int main() { D d; d.foo(); // will resolve to D::foo no matter what d.B::foo(); // explicit specification of what to call }
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sean Don |
last post by:
Hello!
Is it possible to make classes friends in Python? Or is it better to just stick
to "Java style" .isThis, .getThat, .setThis style functions?
I understand that classes are generally...
|
by: Alan |
last post by:
On the book C# for Experienced Programmer by Deitel, page 152, Software
Engineering Observation 5.13:
'When one object of a class has a reference to another object of the same
class, the first...
|
by: WXS |
last post by:
I know this sounds contrary to the idea of an interface, but read this and
see what you think.
-----------------------------------------------------------------------------------------
It would be...
|
by: jopperdepopper |
last post by:
Hi,
finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?
I quote the php manual: "The visibility of a property or...
|
by: time.swift |
last post by:
Coming from a C++ / C# background, the lack of emphasis on private data
seems weird to me. I've often found wrapping private data useful to
prevent bugs and enforce error checking..
It appears...
|
by: Yarco |
last post by:
For example:
<?php
class Test
{
private $name = 'yarco';
}
$p = new ReflectionPropery('Test', 'name');
print $p->getValue();
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |