473,398 Members | 2,812 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,398 software developers and data experts.

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 2685
On Dec 27, 8:53 pm, Mark <mnbaya...@gmail.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(false) { }
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*******@gmail.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...@gmail.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(false) { }
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...because
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.place...@sign.here.pobox.comwrote:
On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmail.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...@gmail.comwrote:
On Dec 27, 7:44 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 27, 8:53 pm, Mark <mnbaya...@gmail.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(false) { }
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...because
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<iostream>

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

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

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

/*
Parent::do_something()
*/

Dec 28 '07 #6
On 28 ΔΕΛ, 04:53, Mark <mnbaya...@gmail.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*******@gmail.comsaid:
On Dec 27, 9:50 pm, Dave Rahardja
<drahardja.place...@sign.here.pobox.comwrote:
>On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmail.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
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...
3
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
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
1
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...
20
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...
6
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...
7
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...
17
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;"...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.