Peter Cranz wrote:
[code snip>
at implementation I'd like to call A::X::f() inside A::B::X::f()
I have tried the following (but it doesn't work .. can't get access of
protected element f() inside a::X <- confusing, its the parent ^^) :
<code snip>
How can I get access to the parental f()-function from inside the
childfunction-f()? I must use these namespaces and also the identical
classnames. :(
Hi Peter,
I know very little. I will tell what I know.
if you create in either identical namespaces or in different
namespaces, you are using virtual. That is more important.
namespace A {
class X {
protected:
// public:
virtual void f() { cout << "A::X::f()" << endl; }
void g() { cout << "A::X::g()" << endl; }
};
}
namespace A {
namespace B {
class X : public A::X {
public:
void f() {
cout << "A::B::X::f()" << endl;
(this->*A::X::f)(); // will again call child class's fn
}
void g() {
cout << "A::B::X::g()" << endl;
(this->*A::X::g)(); // will call parent's fn
// X::g(); // this will work if names of Parent and child are diff.
}
};
}
}
int main(int argc, char* argv[])
{
A::B::X xObj;
// xObj.f();
xObj.g();
return 0;
}
if you run this, you will get fn g() in derived as well as in base
class.
if you remove comments on xObj.f(), you will find repetitive calls of
Child's f() fn. This is because base class virtual table function
always points to it's child class over-ridden functions.
/* point out of scope */
if you use static_cast on this, you will not be able to access parent's
function because they are protected members.
so if you make them public, again child class's functions are called.
So finally I am not able to call virtual function of a base class "or"
can I say it is not possible?
-- Murali Krishna.