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

Catching overriding methods with wrong signature

Hi,

I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
class Base
{
public:
virtual void Foo() const;
}

class Derived : public Base
{
public:
virtual void Foo() const;
}
If I want to change Foo() to a non-const method I'll have to find all the
places where Foo() is overridden and change those as well. A daunting task
in a big project! And quite possible that some may be missed.

The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.

Now, I haven't seen this in any FAQs, but if I've missed one that describes
my solution then I swear it's coincidence! :-)

If I changed Base::Foo to be non-const, but left Derived::Foo const, the
compiler would not warn me. Fair enough. But, if I make Base:

class Base
{
public:
virtual void Foo();

class NotOverridable;
virtual NotOverridable* Foo() const { return 0; }
}
The compiler is nice enough to tell me the return type differs and is not
co-variant. Of course, NotOverridable should not be defined, and the method
still needs a body, otherwise a link error occurs because it's virtual. (Is
there any way around this?)

Does this seem like a good idea? It seems to work for my case, but is there
likely to be anything I've missed that might make this bad on a wider scale?
--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Apr 28 '07 #1
9 1819
* Steve Folly:
Hi,

I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
class Base
{
public:
virtual void Foo() const;
}

class Derived : public Base
{
public:
virtual void Foo() const;
}
If I want to change Foo() to a non-const method I'll have to find all the
places where Foo() is overridden and change those as well. A daunting task
in a big project! And quite possible that some may be missed.

The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.

Now, I haven't seen this in any FAQs, but if I've missed one that describes
my solution then I swear it's coincidence! :-)

If I changed Base::Foo to be non-const, but left Derived::Foo const, the
compiler would not warn me. Fair enough. But, if I make Base:

class Base
{
public:
virtual void Foo();

class NotOverridable;
virtual NotOverridable* Foo() const { return 0; }
}
The compiler is nice enough to tell me the return type differs and is not
co-variant. Of course, NotOverridable should not be defined, and the method
still needs a body, otherwise a link error occurs because it's virtual. (Is
there any way around this?)

Does this seem like a good idea?
For finding the earlier overrides, yes. Neat trick. Just be sure you
rebuild all.

It seems to work for my case, but is there
likely to be anything I've missed that might make this bad on a wider scale?
It wouldn't be nice to have that constraint imposed permanently.

I'd remove it after finding & correcting the earlier overrides, unless
instructed to not use any time at all on structuring for maintenance.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 28 '07 #2
On 28/4/07 12:03, in article 59*************@mid.individual.net, "Alf P.
Steinbach" <al***@start.nowrote:

For finding the earlier overrides, yes. Neat trick. Just be sure you
rebuild all.
Good point! (Visual Studio 2005 sometimes has a knack of not bothering to
recompile something that needs to be recompiled!)
It wouldn't be nice to have that constraint imposed permanently.

I'd remove it after finding & correcting the earlier overrides, unless
instructed to not use any time at all on structuring for maintenance.
Yes, that's what I thought. Unfortunately the code is in a common library
shared by many projects. I would rather not keep them permanently, but
rather than having to check each project now I will let each project accept
the changes to the common library and they can do it on their own slower
time.

I suppose I could take them out eventually...

Many thanks.

--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Apr 28 '07 #3
On Sat, 28 Apr 2007 09:51:41 GMT, Steve Folly wrote:
>I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
What about making the const version private?
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 28 '07 #4
* Roland Pibinger:
On Sat, 28 Apr 2007 09:51:41 GMT, Steve Folly wrote:
>I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:

What about making the const version private?
It can still be overridden. Access is orthogonal to "overridability".
The C++ experts who are religious about this are about evenly divided on
whether you should always make virtual member functions private, or
never make them private; the FAQ seems to be in the latter camp.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 28 '07 #5
On Sat, 28 Apr 2007 13:52:14 +0200, "Alf P. Steinbach" wrote:
>* Roland Pibinger:
>What about making the const version private?

It can still be overridden. Access is orthogonal to "overridability".
Ok, at least it would give a compile-time error in the above case
whenever the function is called polymorphically. A better approach is
to change

class Base
{
public:
virtual void Foo() const;
};

to

class Base
{
public:
virtual void Foo() = 0;
};

Now he gets a compile-time error (when Foo() is called on a const Base
object) and a linker error for derived classes that don't implement
the non-const function.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 29 '07 #6
On Sat, 28 Apr 2007 09:51:41 GMT, Steve Folly wrote:
>Hi,

I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
class Base
{
public:
virtual void Foo() const;
}

class Derived : public Base
{
public:
virtual void Foo() const;
}
If I want to change Foo() to a non-const method I'll have to find all the
places where Foo() is overridden and change those as well. A daunting task
in a big project! And quite possible that some may be missed.

The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.
Actually, I'd expect any quality implementation to warn about the
function in the derived class hiding the (non-const) one in its base.
What compiler are you using?

--
Gennaro Prota
https://sourceforge.net/projects/breeze/
Apr 29 '07 #7
On 29/4/07 11:25, in article 2d********************************@4ax.com,
"Gennaro Prota" <address@spam_this.comwrote:
On Sat, 28 Apr 2007 09:51:41 GMT, Steve Folly wrote:
>Hi,

I had cause to change a base class virtual method's signature from const to
non-const and realised the compiler didn't warn me about derived classes
that hadn't changed:
class Base
{
public:
virtual void Foo() const;
}

class Derived : public Base
{
public:
virtual void Foo() const;
}
If I want to change Foo() to a non-const method I'll have to find all the
places where Foo() is overridden and change those as well. A daunting task
in a big project! And quite possible that some may be missed.

The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.

Actually, I'd expect any quality implementation to warn about the
function in the derived class hiding the (non-const) one in its base.
What compiler are you using?
Microsoft Visual Studio 2005

--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Apr 29 '07 #8
On 29/4/07 10:26, in article 46**************@news.utanet.at, "Roland
Pibinger" <rp*****@yahoo.comwrote:
On Sat, 28 Apr 2007 13:52:14 +0200, "Alf P. Steinbach" wrote:
>* Roland Pibinger:
>>What about making the const version private?

It can still be overridden. Access is orthogonal to "overridability".

Ok, at least it would give a compile-time error in the above case
whenever the function is called polymorphically. A better approach is
to change

class Base
{
public:
virtual void Foo() const;
};

to

class Base
{
public:
virtual void Foo() = 0;
};

Now he gets a compile-time error (when Foo() is called on a const Base
object) and a linker error for derived classes that don't implement
the non-const function.
I'm not sure this is better; derived classes don't have to override the
method. But I do want to catch them overriding the method with the wrong
signature.

--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Apr 29 '07 #9
On Sun, 29 Apr 2007 12:57:32 GMT, Steve Folly wrote:
>>The ones I missed would of course be found eventually by testing, but if I
could catch them at compile time, so much the better.

Actually, I'd expect any quality implementation to warn about the
function in the derived class hiding the (non-const) one in its base.
What compiler are you using?

Microsoft Visual Studio 2005
Hmm :-( One can imagine various do-it-yourself solutions but... if you
have a chance to use gcc, give it a run using its -Woverloaded-virtual
option. The Intel compiler, if you have it, will also detect such
potential mistakes out of the box (and it will, more likely than gcc,
compile your Windows/MSCV-biased code on the spot, but of course it is
not free).

--
Gennaro Prota
https://sourceforge.net/projects/breeze/
Apr 29 '07 #10

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

Similar topics

15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
3
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any...
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
4
by: Aamir Mahmood | last post by:
Hi all, I have a question. Can static members (methods and nested types) be made virtual in a class? So that they can be overridden in the child classes. For example: -------------------...
4
by: TS | last post by:
i have a class that i'm trying to understand that overrides BaseApplicationException's methods as follows. What i dont' understand is that i have never seen the inherit ":" on a method signature,...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
17
by: Donn Ingle | last post by:
Hi, Here's a framework for the questions: --- In a module, part of an API --- class Basis ( object ): def foo ( self, arg ): pass --- In user's own code --- class Child ( Basis ):
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: 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
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
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
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,...
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.