473,753 Members | 7,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11441
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.p ol.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 myinheritingcla ss : 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
7383
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 { virtual void func(); } void some_routine() { B b;
2
1918
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 pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
6
10974
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; public: std::string& getStr(){return str;}
0
1178
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 tlb file Problem: (A) Everyrthing works fine except that I get a compile time error C2039 while trying to
5
3434
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
12
1984
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
3449
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 vtable pointer is made use of.. eg. class one {
6
4138
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 functions of another (same type) - but not via a base class pointer.... I've checked the FAQs, Meyers etc but nothing obvious I can find explains it.
1
6533
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 Talker() in Parent will call the Talker() method in every child class. The kicker is that I have many different Child classes, and not all Child classes will be loaded when Talker() in the Parent is called. Thanks, Randy
0
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6869
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.