473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

privatising public member functions in derived classes.

Hello

#include <iostream>
using std::cout;

struct A {
~virtual A(){}
virtual void foo(){ cout << "aaa\n"; }
};

struct B : A {
private:
virtual void foo(){ cout << "bbb\n"; }
};

void bar(A& ab)
{
ab.foo();
}
int main()
{
A a;
B b;
bar(b)
}

The above code works properly. As far as I know LSP is not being
violated here. So why FAQ lite says this:
http://www.parashift.com/c++-faq-lit....html#faq-21.1
Nov 21 '07 #1
2 1180
Firkraag wrote:
#include <iostream>
using std::cout;

struct A {
~virtual A(){}
virtual void foo(){ cout << "aaa\n"; }
};

struct B : A {
private:
virtual void foo(){ cout << "bbb\n"; }
};

void bar(A& ab)
{
ab.foo();
}
int main()
{
A a;
B b;
bar(b)
}

The above code works properly. As far as I know LSP is not being
violated here. So why FAQ lite says this:
http://www.parashift.com/c++-faq-lit....html#faq-21.1
Compile this version of 'main' instead:

template<class Tvoid callFoo(T &t)
{
t.foo();
}

int main()
{
B b;
callFoo(b);
}

Now, the "duck typing" doesn't work, does it? LSP or not, what is
the point of hiding 'foo' in 'B'? If you didn't intend to call the
'foo' member directly, you ought to make it private and keep it
private:

struct A {
private:
virtual void foo() { cout << "aaa\n"; }
public:
void doFoo() { foo(); }
};

struct B : A {
private:
virtual void foo() { cout << "bbb\n"; }
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '07 #2
Firkraag wrote:
}

The above code works properly. As far as I know LSP is not being
violated here. So why FAQ lite says this:
http://www.parashift.com/c++-faq-lit....html#faq-21.1
First the name is looked up.
Then overloads for the found name are considered.
Then the access for the best overload is considered.

THEN, if the function is virtual, the final overrider is
invoked.
As with most everything else in C++, the thing that matters
is the static declaration at the pointer of the call.
Nov 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Aarno | last post by:
Can anyone provide a good explanation to why the following does not work? After all since A is a public base of B objects of type B ISA object of type A. class A { protected: void foo() {} };...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
1
by: John M. King | last post by:
Hi, I have the following code structure, where the base class's constructor takes a SomeClass pointer and stores it internally in a member. Each derived class needs to have access to that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.