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