473,657 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1827
* 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.no wrote:

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_t his.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.a t, "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
24001
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 overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a real world example of using virtual functions would be very much appreciated.
3
4795
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 differences between these two methods of overriding? Thanks.
4
1927
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 the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
4
8764
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: ------------------- public class Parent { public enum AllowedColors = {Red, Blue, Green} public static void DoSomething() {
4
1458
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, only on a class declaration. Can you explain what is going on here. Is it that the implementation of the method that is overriding the base's method simply uses the base's implementation all along (if so, why override the method to begin with?)?
3
1546
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
27797
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 was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if it has arguments. (example ** assuming the Person class's constructor has (string FirstName) as...
10
105177
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
17
1683
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
8399
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
8732
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
8504
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
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6169
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
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
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
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
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.