473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

calling parent virtual function wtih out calling it

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
3 11418
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
"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
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 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 {...
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...
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;...
0
by: Maansi Sanghi | last post by:
Hello, (1) I am building a com component with multiple interfaces in .NET Managed VC++ (2) Then use the managed .NET dll in unmanaged code after registering through regasm and getting the...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.