472,784 Members | 1,235 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,784 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 11340
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
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...

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.