By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

calling a virtual funktion from a parent class

P: n/a
hello,

I've got the following problem:

I have a construct similar like this:
namespace A {

class X {

protected:

virtual void f();
}
}
....

namespace A {

namespace B {

class X : public A:X {

protected:

virtual void f();
}
}
}

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 ^^) :

namespace A {
namespace B {
X::f() { ...
using A::X;
X::f();
}
}
}
... also I've tried this (but it doesn't work .. it never calls f()
from parent class):

namespace A {
namespace B {
X::f() { ...
X::X::f();
}
}
}
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. :(
Jul 4 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Peter Cranz schrieb:
hello,

I've got the following problem:

I have a construct similar like this:
namespace A {

class X {

protected:

virtual void f();
}
}
....

namespace A {

namespace B {

class X : public A:X {

protected:

virtual void f();
}
}
}

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 ^^) :

namespace A {
namespace B {
X::f() { ...
using A::X;
X::f();
}
}
}
... also I've tried this (but it doesn't work .. it never calls f()
from parent class):

namespace A {
namespace B {
X::f() { ...
X::X::f();
}
}
}
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. :(
I'm using visual c++ ^^
Jul 4 '06 #2

P: n/a
Peter Cranz schrieb:
hello,

I've got the following problem:
[...]
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 ^^) :

namespace A {
namespace B {
X::f() { ...
using A::X;
X::f();
}
}
}
... also I've tried this (but it doesn't work .. it never calls f()
from parent class):

namespace A {
namespace B {
X::f() { ...
X::X::f();
}
}
}
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. :(
A complete compilable example would help.

Did you try either:

A::X::f(); or this->A::X::f();

in f() of the subclass?

--
Thomas
Jul 4 '06 #3

P: n/a
so here are the classes .. but they are only a piece inside a complex
hierarchy:

....................... the parent ...............

namespace omxj
{

class GateConnectCallback : public JavaObjectFrame
{
private:

std::string getOMXGatePath_Name;
std::string getOMXUserId_Name;
std::string getOMXPwId_Name;
std::string getOMXApplId_Name;

protected:

virtual void initialize();

jmethodID getOMXGatePath_Mid;
jmethodID getOMXUserId_Mid;
jmethodID getOMXPwId_Mid;
jmethodID getOMXApplId_Mid;

std::string getMethod_Signature;

GateConnectCallback():
getOMXGatePath_Name("getOMXGatePath"),
getOMXUserId_Name("getOMXUserId"),
getOMXPwId_Name("getOMXPwId"),
getOMXApplId_Name("getOMXApplId"),
getMethod_Signature("()Ljava/lang/String;"){};

public:

virtual void getDataFromGateConnect(JavaPackageInfoT *request,
GateConnectInfoT *gateConnectStruct);
};

};

................ and the child .................

namespace omxj
{
namespace xetra
{

class GateConnectCallback : public omxj::GateConnectCallback
{
protected:

virtual void initialize();

public:

virtual void getDataFromGateConnect(JavaPackageInfoT *request,
GateConnectInfoT *gateConnectStruct);
};

};

};
... inside omxj::xetra::GateConnectCallback::initialize() I must get
access to xetra::GateConnectCallback::initialize()

.......... the implementation of the childclass .............

namespace omxj
{
namespace xetra
{

void GateConnectCallback::initialize()
{
// something to do inside this child
omxj::GateConnectCallback::initialize();
}

void GateConnectCallback::getDataFromGateConnect(JavaPa ckageInfoT *request,
GateConnectInfoT *gateConnectStruct)
{
GateConnectCallback::GateConnectCallback::getDataF romGateConnect(request,
gateConnectStruct);
}

}

}

hope that is enough explanation ... a compilable example is not available :(
Jul 4 '06 #4

P: n/a
Thomas J. Gritzan schrieb:
Peter Cranz schrieb:
>hello,

I've got the following problem:
[...]
>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 ^^) :

namespace A {
namespace B {
X::f() { ...
using A::X;
X::f();
}
}
}
... also I've tried this (but it doesn't work .. it never calls f()
from parent class):

namespace A {
namespace B {
X::f() { ...
X::X::f();
}
}
}
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. :(

A complete compilable example would help.

Did you try either:

A::X::f(); or this->A::X::f();

in f() of the subclass?
yes I did ...

A::X::f() <-- no static element f() inside A::X

this->A::X::f(); <-- error: A isn't a member of X
Jul 4 '06 #5

P: n/a
Peter Cranz schrieb:
namespace omxj
{

class GateConnectCallback : public JavaObjectFrame
{
[snip]
public:

virtual void getDataFromGateConnect(JavaPackageInfoT *request,
GateConnectInfoT
*gateConnectStruct);
};

};

............... and the child .................

namespace omxj
{
namespace xetra
{

class GateConnectCallback : public omxj::GateConnectCallback
{
protected:

virtual void initialize();

public:

virtual void getDataFromGateConnect(JavaPackageInfoT *request,
GateConnectInfoT
*gateConnectStruct);
};

};

};
.. inside omxj::xetra::GateConnectCallback::initialize() I must get
access to xetra::GateConnectCallback::initialize()

......... the implementation of the childclass .............

namespace omxj
{
namespace xetra
{

void GateConnectCallback::initialize()
{
// something to do inside this child
omxj::GateConnectCallback::initialize();
}

void GateConnectCallback::getDataFromGateConnect(JavaPa ckageInfoT *request,
GateConnectInfoT
*gateConnectStruct)
{
GateConnectCallback::GateConnectCallback::getDataF romGateConnect(request,
gateConnectStruct);
Why do you do "GateConnectCallback::GateConnectCallback::" here? There
is no namespace called "GateConnectCallback".

Try:

omxj::GateConnectCallback::getDataFromGateConnect( request,
gateConnectStruct);
}

}

}

hope that is enough explanation ... a compilable example is not
available :(
--
Thomas
Jul 4 '06 #6

P: n/a
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.

Jul 5 '06 #7

P: n/a
Murali Krishna schrieb:
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.
thank you murali,

I think, it's possible to call a protected virtual function of the
base-class from inside the "same" virtual function inside the
child-class. there are no difficulties, if base-class and child-class
has different names. it's no matter what namespace is used. so here is
an example of what I said:

namespace A {
class X {
protected:
virtual void f()
{
cout << "A::X::f()" << endl;
}
public:
void print() {f();}
};
}

namespace A {
namespace B {
class Y : public A::X {
protected:
virtual void f()
{
cout << "A::B::Y::f()" << endl;
using namespace A;
X::f();
}
};
}
}

.....
Y* y = new Y(); y->print(); // will print out the following:
>A::B::Y::f()
A::X::f()
X* x = y; x->print(); // will print out the following:
>A::X::f()
please correct me if I'm wrong.

My only problem is, that I do not know ... how to realize this with same
classnames in different namespaces. so, maybe, I have an understanding
problem of how to use namespaces in c++ or is there a little
inconsistency? In my opinion, namespaces are supposed to be a delimiter
that makes programmers possible, to use same names for different things
in different namespaces. does this not match for inheritance?

Jul 5 '06 #8

P: n/a
Peter Cranz schrieb:
My only problem is, that I do not know ... how to realize this with same
classnames in different namespaces. so, maybe, I have an understanding
problem of how to use namespaces in c++ or is there a little
inconsistency? In my opinion, namespaces are supposed to be a delimiter
that makes programmers possible, to use same names for different things
in different namespaces. does this not match for inheritance?
It does. Please read my previous post.

Just call the function with the _complete_ path:

namespace A { namespace B {
class X
{
public:
void f() {}
}
} }

f() is called by:

A::B::X::f();

--
Thomas
Jul 5 '06 #9

P: n/a
Murali Krishna schrieb:
[...]
>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
}
No. It wouldn't compile. The operator ->* is for accessing by member
pointers. However, even

(this->A::X::f)();

Won't call the child class function, even if f() is virtual. By
specifing explicitly A::X::f you call the function f() in A::X.
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.
}
};
}
}
[...]

--
Thomas
Jul 5 '06 #10

P: n/a
Thomas J. Gritzan schrieb:
Peter Cranz schrieb:
>My only problem is, that I do not know ... how to realize this with same
classnames in different namespaces. so, maybe, I have an understanding
problem of how to use namespaces in c++ or is there a little
inconsistency? In my opinion, namespaces are supposed to be a delimiter
that makes programmers possible, to use same names for different things
in different namespaces. does this not match for inheritance?

It does. Please read my previous post.

Just call the function with the _complete_ path:

namespace A { namespace B {
class X
{
public:
void f() {}
}
} }

f() is called by:

A::B::X::f();
hi thomas,

here is what I've tried and what kind of result I'v got:

namespace A {
class X {
protected:
virtual void f()
{
cout << "A::X::f()" << endl;
}
public:
void print() {f();}
};
}

namespace A {
namespace B {
class X : public A::X {
protected:
virtual void f()
{
cout << "A::B::X::f()" << endl;
using namespace A;
X::f(); // will call A::B::X::f() not A::X::f()
A::X::f(); // compiler error message*
}
};
}
}

compiler error message* : f() is not a static member of A::X

I'm confused about this message, due to A::B::X is a child of A::X :(

I'm using visual studio c++ 6.0 (sp 1)
Jul 5 '06 #11

P: n/a
Peter Cranz schrieb:
hi thomas,

here is what I've tried and what kind of result I'v got:

namespace A {
class X {
protected:
virtual void f()
{
cout << "A::X::f()" << endl;
}
public:
void print() {f();}
};
}

namespace A {
namespace B {
class X : public A::X {
protected:
virtual void f()
{
cout << "A::B::X::f()" << endl;
using namespace A;
X::f(); // will call A::B::X::f() not A::X::f()
A::X::f(); // compiler error message*
}
};
}
}

compiler error message* : f() is not a static member of A::X

I'm confused about this message, due to A::B::X is a child of A::X :(
By adding

#include <iostream>
using namespace std;

and a main() function, I got it compiled on gcc and VC++ 7.1
I'm using visual studio c++ 6.0 (sp 1)
That explains. Don't use this old compiler.

--
Thomas
Jul 5 '06 #12

P: n/a
So that's the reason? microsofts compiler? -.- In future I will try to
say nothing about this and will use another compiler.

@all:
Thank you very much for your effort to help me. :)

regards
peter
Jul 5 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.