472,958 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

calling a virtual funktion from a parent class

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
12 1932
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kris Thielemans | last post by:
Hi I have a rather outlandish problem where I want to call a virtual function which is (sort of) hidden by a derived class. For instance class A { virtual void func(); }; class B: public A {...
7
by: christopher diggins | last post by:
Hi everyone, I have the following code (greatly simplified to demonstrate issue) : #include <iostream> class CBaseFuBar { public: virtual void Fu() { std::cout << "base"; }; };
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
3
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function...
6
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str;...
5
by: claus.hirth | last post by:
If I create the function HELLO in schema S01 as follows, @ CREATE FUNCTION S01.HELLO() RETURNS VARCHAR(32) EXTERNAL NAME 'UDFSRVXYZ!sayHelloWorld' LANGUAGE JAVA PARAMETER STYLE DB2GENERAL NO...
1
by: NOSPAM | last post by:
I have a question on calling parent class... What I want to do is: when calling the derived class, it automatically executes the parent's method, is it possible? // main code: Derived d = new...
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
7
by: Mark | last post by:
Hi, I have an abstract class which I want my other classes to inherit from. In the constructor of the abstract class I want to check if certain virtual functions have been overloaded or not. Is...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.