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

calling parent virtual function wtih out calling it

P: n/a
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 does somthing that must be done. This meens
that when a child class inherits the class and creates its own vertual int
A() the parent class must also be called. the prob is i can not use the
base class name and then its functino name after it from with in the child
class to call it. is there any way to make it so that all the verutal
functions that are made are exectued. The reson its a verutal function is
becase i only have the memmory address of the parent class but i need to
call the highest child classes function. this meens that when i call the
parents class verutal function it actualy calls the highest childs vertual
funtion instead. But then i want it to call all its parent function in turn
after it.

Hope what iv said makes sence and hope some one can help.

Thx for any help given.

Scott.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
scott wrote:
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 does somthing that must be done. This meens
that when a child class inherits the class and creates its own vertual int
A() the parent class must also be called. the prob is i can not use the
base class name and then its functino name after it from with in the child
class to call it. is there any way to make it so that all the verutal
functions that are made are exectued. The reson its a verutal function is
becase i only have the memmory address of the parent class but i need to
call the highest child classes function. this meens that when i call the
parents class verutal function it actualy calls the highest childs vertual
funtion instead. But then i want it to call all its parent function in turn
after it.

Hope what iv said makes sence and hope some one can help.

Thx for any help given.

Scott.


Take a look at the Template Method pattern. The basic idea is that the
base class has a function that looks something like this:

class C{
public:

void do_something(){
set_up_state();
vfunc();
clean_up();
}
virtual vfunc() = 0;
};

class Q: public C{
public:
virtual vfunc(){
// ...
}
};

In your code you then instantiate a Q object and call its do_something()
function. That function can do all the pre/post process that the base
class requires, with a call to vfunc() in the middle.

Just for the record, there shouldn't be any problem with calling the
parent's virtual function from the child. What error were you seeing?

HTH,
Jacques.
Jul 22 '05 #2

P: n/a
"scott" <sc***********@hotmail.com> wrote in message
news:ck**********@newsg3.svr.pol.co.uk...
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 does somthing that must be done. This meens
that when a child class inherits the class and creates its own vertual int
A() the parent class must also be called. the prob is i can not use the
base class name and then its functino name after it from with in the child
class to call it. is there any way to make it so that all the verutal
functions that are made are exectued. The reson its a verutal function is becase i only have the memmory address of the parent class but i need to
call the highest child classes function. this meens that when i call the
parents class verutal function it actualy calls the highest childs vertual
funtion instead. But then i want it to call all its parent function in turn after it.

Hope what iv said makes sence and hope some one can help.


It sounds like you're looking for the template method pattern. The idea is
simple: replace a single virtual function in the base class with one that is
not virtual and one that is virtual. Have the non-virtual function call the
virtual function. So

struct A {
virtual void foo() {
// Do some work here
}
};

struct B : A {
virtual void foo() {
// Do some more work here
A::foo();
}
};

becomes

struct A {
void foo() {
foo2();
// Do some work here
}
private:
virtual void foo2() {}
};

struct B : A {
private:
virtual void foo2() {
// Do some more work here
}
};

By the way, I don't understand why you "can't" call the base class's member
function from the derived class's member function.

--
David Hilsee
Jul 22 '05 #3

P: n/a
scott wrote:
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 does somthing that must be done. This meens
that when a child class inherits the class and creates its own vertual int
A() the parent class must also be called. the prob is i can not use the
base class name and then its functino name after it from with in the child
class to call it. is there any way to make it so that all the verutal
functions that are made are exectued. The reson its a verutal function is
becase i only have the memmory address of the parent class but i need to
call the highest child classes function. this meens that when i call the
parents class verutal function it actualy calls the highest childs vertual
funtion instead. But then i want it to call all its parent function in turn
after it.


hi scott,

probably you want to do something like this:

class mybaseclass {
virtual void myfunc() {
do something ;
}
} ;
class myinheritingclass : mybaseclass {
virtual void myfunc() {
do some stuff...
mybase::myfunc() ; // <--------- this syntax is probably what you
are looking for
do some more stuff...
}
} ;

the '.' syntax (e.g. this.myfunc()) doesn't work for the obvious reason...

David
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.