473,394 Members | 1,746 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,394 software developers and data experts.

Determine if a function has been overridden

Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it's base class (which
is abstract)? The reason I want to do this is that I want to call this
function if it has been overridden, and not if it hasn't, e.g

class CBase
{
public:
virtual long mightBeOverridden(int i) { return 0; } // not a pure
virtual function, since sub-classes should not have to define it
bool isFunctionOverRidden() { return false; }// This is what I want
to code
virtual int id()=0; // this class is an abstract class because of
this function - I don't think that this is relevant but more info is good
};

class CDerived1
{
// Does not override the function
int id() { return 1; }
};

class Derived2
{
long mightBeOverridden(int i) { return i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 2; }
};

class Derived3
{
long mightBeOverridden(int i) { return i * i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 3; }
};

int foo(CBase* pObj, int i, int j)
{
if(pObj)
{
if(pObj->isFunctionOverridden())
return mightBeOverridden(i);
}

return j;
}
I googled on this and came up with nought, as did a search of this list.
I'm guessing that means it's impossible. I thought that I might
compare function pointers ( return mightBeOverridden !=
CBase::mightBeOverridden ) but that doesn't distinguish between the two.

If it's relevant, I'm using MS Visual C++ 7.0 (aka .NET 2002), due to
upgrade to 8.0 soon.

Thanks in advance,

Stewart
Jun 7 '06 #1
11 2722
Sorry, in my example the Derived classes should be declared as follows.

CDerived1 : public Base

CDerived2 : public Base

CDerived3 : public Base

The problem remains.
Jun 7 '06 #2
S. I. Becker wrote:
Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it's base class (which
is abstract)? The reason I want to do this is that I want to call
this function if it has been overridden, and not if it hasn't, e.g
This seems like an attempt to work around a bad design. Why in the
world would you intentionally design your system like that?
class CBase
{
public:
virtual long mightBeOverridden(int i) { return 0; } // not a pure
virtual function, since sub-classes should not have to define it
bool isFunctionOverRidden() { return false; }// This is what I want
to code
virtual int id()=0; // this class is an abstract class because of
this function - I don't think that this is relevant but more info is
good };

class CDerived1
{
// Does not override the function
int id() { return 1; }
};

class Derived2
{
long mightBeOverridden(int i) { return i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 2; }
};

class Derived3
{
long mightBeOverridden(int i) { return i * i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 3; }
};

int foo(CBase* pObj, int i, int j)
{
if(pObj)
{
if(pObj->isFunctionOverridden())
return mightBeOverridden(i);
You mean

return pObj->mightBeOverridden(i);
}

return j;
}
I googled on this and came up with nought, as did a search of this
list. I'm guessing that means it's impossible. I thought that I
might compare function pointers ( return mightBeOverridden !=
CBase::mightBeOverridden ) but that doesn't distinguish between the
two.
I don't think it's possible without some trick with dynamic_cast which
means that 'foo' has to know about derived classes, which in turn shows
that the design is bad, which suggests that you need to revisit the
design instead of patching it up like that.

The whole point of virtual functions is that they are supposed to be
used *without* any regard to whether the derived class has or hasn't
overridden them. If suddenly you have an urge to learn that, then
you probably shoudn't have those functions virtual in the first place.
If it's relevant, I'm using MS Visual C++ 7.0 (aka .NET 2002), due to
upgrade to 8.0 soon.


It's not.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 7 '06 #3
Agree this will be a bad design, but however if you want to do this,
I haven't try but just simply by theory

class base
{
public:
virtual void overrideFunc() {}
virtual int isFunctionOveride()
{
base refObj;
return (&this->overrideFunc != &refObj.overrideFunc );
}
};

class derive1 : public base
{
public:
virtual void overrideFunc() { /* overrided */ }
};

Jun 8 '06 #4
Victor Bazarov wrote:
S. I. Becker wrote:
Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it's base class (which
is abstract)? The reason I want to do this is that I want to call
this function if it has been overridden, and not if it hasn't, e.g


This seems like an attempt to work around a bad design. Why in the
world would you intentionally design your system like that?


I thought I might get a response like this!

The situation is like that in foo. There is a value that should be
obtained, only if the object has code for how to do it, otherwise
another should be used. Following your comments, I have decided to
change it as follows:

int CBase::mightBeOverridden(int i, int j) // Note, this function now
takes in two arguments
{
i; // avoid compiler warning - will be optimised out
return j;
}

int CDerived2::mightBeOverridden(int i, int j)
{
j; // avoid compiler warning - will be optimised out
return i;
}

int CDerived3::mightBeOverridden(int i, int j)
{
j; // avoid compiler warning - will be optimised out
return i * i;
}

int foo(CBase* pObj, int i, int j)
{
if(pObj)
{
return pObj->mightBeOverridden(i, j);
}

return j;
}

Comments appreciated.

Stewart
Jun 8 '06 #5
S. I. Becker <stewart.becker@nospam> wrote:
The situation is like that in foo. There is a value that should be
obtained, only if the object has code for how to do it, otherwise
another should be used. Following your comments, I have decided to
change it as follows:

int CBase::mightBeOverridden(int i, int j) // Note, this function now
takes in two arguments
{
i; // avoid compiler warning - will be optimised out
return j;
}


Instead of putting in a statement that doesn't really do anything ("i;")
to avoid the compiler warning, I would just comment out the unused
parameters:

int CBase::mightBeOverriddent(int /*i*/, int j)
{
return j;
}

and similarly for the rest.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 8 '06 #6

Patrick wrote:
Agree this will be a bad design, but however if you want to do this,
I haven't try but just simply by theory

class base
{
public:
virtual void overrideFunc() {}
virtual int isFunctionOveride()
{
base refObj;
return (&this->overrideFunc != &refObj.overrideFunc );
}
};


Won't work in his case because base has pure virtual methods so you
can't create an instance of it.

What might work is to go out and create (privately) a class that
derives from base and does not override the method, and then do what
you tried. Don't know if that will work though.

I thought OP's method might work if he instead tried:

&this->overrideFunc != &base::overrideFunc

Jun 8 '06 #7

S. I. Becker wrote:
Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it's base class (which
is abstract)?
highlighting on:
The reason I want to do this is that I want to call this
function if it has been overridden, and not if it hasn't, e.g


But if the base-class implementation is empty (does nothing) then just
call it and if it wasn't implemented you haven't lost anything.

It is usually better anyway if the base class does nothing.

Perhaps your base class function could take a (non-const) int reference
as a parameter instead of returning an int. Then it can "do nothing"
with the reference. (Doesn't have to set it to 0). Is that the design
you are looking for?

Jun 8 '06 #8
Earl Purple wrote:
But if the base-class implementation is empty (does nothing) then just
call it and if it wasn't implemented you haven't lost anything.

It is usually better anyway if the base class does nothing.
Agreed.
Perhaps your base class function could take a (non-const) int reference
as a parameter instead of returning an int. Then it can "do nothing"
with the reference. (Doesn't have to set it to 0). Is that the design
you are looking for?


Thanks for your comments. See my new implementation:

int CBase::mightBeOverridden(int i, int j) { return j; }
// Overrides in CDerived# also take 2 args

Stewart
Jun 12 '06 #9

S. I. Becker wrote:
Thanks for your comments. See my new implementation:

int CBase::mightBeOverridden(int i, int j) { return j; }
// Overrides in CDerived# also take 2 args


That wasn't what I had in mind. Actually:

bool CBase::mightModifyOutput( int input, int & output )
{
// the function may modify output or may do nothing with it
return false; // return true if it modified output
}

Jun 12 '06 #10
Earl Purple wrote:
S. I. Becker wrote:
Thanks for your comments. See my new implementation:

int CBase::mightBeOverridden(int i, int j) { return j; }
// Overrides in CDerived# also take 2 args


That wasn't what I had in mind. Actually:

bool CBase::mightModifyOutput( int input, int & output )
{
// the function may modify output or may do nothing with it
return false; // return true if it modified output
}


I realise it's not the same, but it does have similar behaviour - it's
just a question of where the output comes from - return value or reference.

Thanks for your help.

Stewart

Jun 13 '06 #11

S. I. Becker wrote:
Earl Purple wrote:
S. I. Becker wrote:
Thanks for your comments. See my new implementation:

int CBase::mightBeOverridden(int i, int j) { return j; }
// Overrides in CDerived# also take 2 args


That wasn't what I had in mind. Actually:

bool CBase::mightModifyOutput( int input, int & output )
{
// the function may modify output or may do nothing with it
return false; // return true if it modified output
}


I realise it's not the same, but it does have similar behaviour - it's
just a question of where the output comes from - return value or reference.

Thanks for your help.


The difference is that your calling code can call the function (using
its base-class with full polymorphism) and know whether the result (the
2nd parameter which is a non-const reference) was modified by checking
the return value. Although that is not quite the same as knowing if the
base class function was overridden (another class could presumably
override the function, not modify the reference and return false) it
will give you the behaviour you actually want.

To answer the very original post, you could have the base class return
false and all derivations return true, although I doubt your client
code (that uses the base class) will find that particularly useful.
(Whether an output reference is modified is useful in some situations).

Jun 14 '06 #12

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

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
1
by: JP | last post by:
Hi, I am facing a strange problem, please take a look at the code below: public SaveTree() { ........ ...... ......//some code Initranges()
3
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base...
8
by: Allan Ebdrup | last post by:
I'm writing some code where I have have a class that implements 4 methods (class A) I only want to call these methods from the base class if they have been overridden in a sub class (Class B) I...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
6
by: wink | last post by:
I'd like to determine if a method has been overridden as was asked here: http://www.velocityreviews.com/forums/t564224-determining-whether-a-derived-class-overrides-a-virtual-memberfunction.html...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.