473,804 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if virtual function has been overloaded

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 this
possible?

For example, let's say I have

class Base {
public:
virtual void display();
};

class Parent: public: Base {
public:
void display()
}

Then what I want to do is this:

Base::Base() {
if(Parent class has defined display) {
// do something
} else {
// do something else
}
}
Dec 28 '07 #1
7 2710
On Dec 27, 8:53 pm, Mark <mnbaya...@gmai l.comwrote:
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 this
possible?
Its not an overload, its an override.
>
For example, let's say I have

class Base {
public:
virtual void display();

};

class Parent: public: Base {
class Parent : public Base
{
public:
void display()

}
;
>
Then what I want to do is this:

Base::Base() {
if(Parent class has defined display) {
// do something} else {

// do something else

}
}
The situation you describe suggests a design problem. If Base needs to
know what interface Parent has, then the variation in Base should
really be in Parent.
That or you need an overload of Base's constructor (not override).
Override means hide, overload means supply an alternate signature.

silly example:

class Base
{
bool has_override;
public:
Base() : has_override(fa lse) { }
Base(bool b) : has_override(b) { do something(); }
virtual void display() const { }
private:
void do_something()
{
if(has_override )
// do this
else
// do that
}
};

class Parent : public Base
{
public:
Parent() : Base(true) { }
void display() const { }
};


Dec 28 '07 #2
On 2007-12-27 19:53:48 -0600, Mark <mn*******@gmai l.comsaid:
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 this
possible?
Why do you want to do this?

-dr

Dec 28 '07 #3
On Dec 27, 7:44 pm, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 27, 8:53 pm, Mark <mnbaya...@gmai l.comwrote:
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 this
possible?

Its not an overload, its an override.
For example, let's say I have
class Base {
public:
virtual void display();
};
class Parent: public: Base {

class Parent : public Base
{


public:
void display()
}
;
Then what I want to do is this:
Base::Base() {
if(Parent class has defined display) {
// do something} else {
// do something else
}
}

The situation you describe suggests a design problem. If Base needs to
know what interface Parent has, then the variation in Base should
really be in Parent.
That or you need an overload of Base's constructor (not override).
Override means hide, overload means supply an alternate signature.

silly example:

class Base
{
bool has_override;
public:
Base() : has_override(fa lse) { }
Base(bool b) : has_override(b) { do something(); }
virtual void display() const { }
private:
void do_something()
{
if(has_override )
// do this
else
// do that
}

};

class Parent : public Base
{
public:
Parent() : Base(true) { }
void display() const { }

};
Yeah... I realized I meant "override" just after posting it, but it's
kind of impossible to edit.
So I'd have to use booleans then? I was hoping I wouldn't...beca use
then I need about 10, and it just gets messy. Oh well, thanks.
Dec 28 '07 #4
On Dec 27, 9:50 pm, Dave Rahardja
<drahardja.plac e...@sign.here. pobox.comwrote:
On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmai l.comsaid:
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 this
possible?

Why do you want to do this?

-dr
Well... if you're curious.

If the Parent class has defined the function, I want to use that
function. If the Parent class has NOT defined the function, I *don't*
want to use the Base class' function, instead, I want to re-route to
another function in a stack. If I'm going to re-route it, I want to
pop an item off the stack, but then I pop an item off the stack in the
destructor, and I don't want to pop it off twice... so... I need to
know whether or not it's already been popped off. Yes, I could use
booleans, but I was hoping there was a more elegant way. In fact... I
did find another way that could potentially stop some future problems
I didn't originally anticipate.

That problem didn't really make a heck of a lot of sense, but I'd have
to explain most of my project before it would make any sense at all.
Dec 28 '07 #5
On Dec 28, 1:03 am, Mark <mnbaya...@gmai l.comwrote:
On Dec 27, 7:44 pm, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 27, 8:53 pm, Mark <mnbaya...@gmai l.comwrote:
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 this
possible?
Its not an overload, its an override.
For example, let's say I have
class Base {
public:
virtual void display();
};
class Parent: public: Base {
class Parent : public Base
{
public:
void display()
}
;
Then what I want to do is this:
Base::Base() {
if(Parent class has defined display) {
// do something} else {
// do something else
}
}
The situation you describe suggests a design problem. If Base needs to
know what interface Parent has, then the variation in Base should
really be in Parent.
That or you need an overload of Base's constructor (not override).
Override means hide, overload means supply an alternate signature.
silly example:
class Base
{
bool has_override;
public:
Base() : has_override(fa lse) { }
Base(bool b) : has_override(b) { do something(); }
virtual void display() const { }
private:
void do_something()
{
if(has_override )
// do this
else
// do that
}
};
class Parent : public Base
{
public:
Parent() : Base(true) { }
void display() const { }
};

Yeah... I realized I meant "override" just after posting it, but it's
kind of impossible to edit.
So I'd have to use booleans then? I was hoping I wouldn't...beca use
then I need about 10, and it just gets messy. Oh well, thanks.
You don't have to. The boolean is a solution akin to a concept_check.

1) You want Parent::display () to be called when its available
2) when its not, you don't want to process Base::display()
3) you'ld like an alternate function to be processed instead

I'ld like to point out that Base::display() has complete access to the
hierarchy's interface.
It can call any member function it has access to with respect to
override rules.
A Parent knows its a Parent, there is no need to tell it.

So lets see what happens with no override...

#include<iostre am>

class Base
{
public:
Base() { }
virtual ~Base() { }
virtual void display()
{
do_something();
}
private:
virtual void do_something()
{
std::cout << "Base::do_somet hing()\n";
}
};

class Parent : public Base
{
public:
Parent() { }
// void display() { }
void do_something()
{
std::cout << "Parent::do_som ething()\n";
}
};

int main()
{
Parent parent;
parent.display( );
}

/*
Parent::do_some thing()
*/

Dec 28 '07 #6
On 28 ÄÅË, 04:53, Mark <mnbaya...@gmai l.comwrote:
>
Then what I want to do is this:

Base::Base() {
if(Parent class has defined display) {
// do something} else {

// do something else

}
}
A rude, ugly misconception. The class' constructor doesn't and must
not know that class is or even can ever be derived. At the stage of
constructing Base we do not know whether it is `in-charge' or is not.
This stuff is left for compilers and user - you - have to _create_
every class like a final one.

I've underlined `create' word because, in opposite, the same doesn't
hold for destroying. So you can provide a mecahnism of informing base
class whether he should `pop item from a stack' in destructor. Just
add a corresponding boolean being set in Parent's virtual destructor.
And that re-routing is done by default in Base's virtual function
`default' body.
Dec 28 '07 #7
On 2007-12-28 00:12:40 -0600, Mark <mn*******@gmai l.comsaid:
On Dec 27, 9:50 pm, Dave Rahardja
<drahardja.plac e...@sign.here. pobox.comwrote:
>On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmai l.comsaid:
>>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 this
possible?

Why do you want to do this?

-dr

Well... if you're curious.

If the Parent class has defined the function, I want to use that
function. If the Parent class has NOT defined the function, I *don't*
want to use the Base class' function, instead, I want to re-route to
another function in a stack. If I'm going to re-route it, I want to
pop an item off the stack, but then I pop an item off the stack in the
destructor, and I don't want to pop it off twice... so... I need to
know whether or not it's already been popped off. Yes, I could use
booleans, but I was hoping there was a more elegant way. In fact... I
did find another way that could potentially stop some future problems
I didn't originally anticipate.

That problem didn't really make a heck of a lot of sense, but I'd have
to explain most of my project before it would make any sense at all.
You have a design problem here. The fact that Base needs to know if
Parent has overridden a virtual function is indicative of a
pathological dependency.

Try looking for a different way to segregate your algorithm. Your Base
class should be a complete class, independent of any potential derived
classes. I suspect that what you'd have to do is to create a different
(or additional) virtual functions, so that Parent can simply extend,
and not modify, Base's management of the pop/no-pop state.

-dr

Dec 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1816
by: Karsten Hochkirch | last post by:
Hi, I have just encountered a problem when calling a virtual member function in a constructor. Only the memberfunction of the parent class is called instead of the overloaded function. I have attached a small example. Is that bug or feature. If feature: is there an easy way to get the
3
7142
by: PengYu.UT | last post by:
The following program shows that virtual function can not be overloaded. Could you tell me the reasons from the C++ compiler point of view? Thanks! Peng #include <iostream>
12
21242
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
1
6652
by: ravinderthakur | last post by:
hi all experts, i was just thinking about making overloaded operators virtual. i was wordering what could be the implications of such scenarios. i will be thankful if somebody can provide point out some general issues in making overloaded virtual or point me to some resource having explaination about the same.
20
4964
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got a VIRTUAL function createList(). This createList() function is overloaded in another class named ct_server that inherits gs_object. in my code, it looks something like that :
6
2279
by: Bit byte | last post by:
I have a class BaseApplication, from which I am deriving two classes A and B. In class BaseApplication, I have a virtual method as ff: virtual void saveToDb(const char*); I will like to overload this function in A and B, because I want to have the ff (for example)
7
2481
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it has nothing to do with making multiple arguments in a declaration like:
17
3554
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
2
3073
by: nagesh0280 | last post by:
Hello Experts, I was wondering (I'm absolutely new to OOP) if virtual functions are limited in their capability in a sense? Consider the following scenario. #include <iostream> using namespace std; class base {
0
9711
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
10343
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...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
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
7633
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
6862
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();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.