Re: Private implementation of public pure virtual
tarjei.knapstad@gmail.com wrote:
The recent thread "Sanity check: public/private" led me to try
something out which unexpectedly succeeded:
/////////////////////////
#include <iostream>
class B
{
public:
virtual ~B() {}
virtual void f() = 0;
};
class D : public B
{
public:
virtual ~D() {}
private:
virtual void f() { std::cout << "D::f()" << std::endl;}
};
int main()
{
D d;
B* p = &d;
p -> f();
}
/////////////////////////
Hello!
Its all right with this code.
The point is that compiller performs static check to find out whether
the function "f()" is accessible.
In other words, your object on which the pointer "p" points, has 2
types - a static type and a dynamic type. Static means - type of object
on compilation phase.
Dynamic means type of object in runtime.
Static type of object - is "B".
Dynamic type of object is "D".
So, to check (its compile time check) whether the function is exist and
accessible, the static type of object is used.
And to call this function in runtime, dynamic type is used (since the
function is virtual) - dynamic type of the object is "D" (VTLB pointer
points to VTLB of class "D")
So the behavior is correct from the point of view of C++ standard.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]