Article View: comp.sys.mac.programmer.codewarrior
Article #12C++ comment on warnings about overloaded functions (Bug?)
From: Julian Harris
Date: Wed, 18 Oct 1995 01:06
Date: Wed, 18 Oct 1995 01:06
66 lines
2506 bytes
2506 bytes
Version: MW IDE 1.3, 68k C++ 1.3 running under System 7.5.1 on Quadra 700.
Reproducability: ALWAYS
Type: gotcha :)
Take this code below. The subclassed virtual function omits the 'const'.
This appears to MW C++ as a new overloaded function (even though this is
the only difference) and hence the 'OverrideMe()' function doesn't behave
polymorphically as one may at first look expect.
Most compilers return a warning when a subclass overloads a parent
function that differs only in return type. Could I suggest that when the
return type differs only in 'const' that this could be flagged as a
warning as well?
FYI, gnu g++ 2.7.0 also gives similar results.
----
#include <iostream.h>
template< class X > class T
{
public:
virtual void CallMe( X a )
{
cout << "CallMe from T" << a << endl;
OverrideMe( a );
}
protected:
virtual void OverrideMe( X a ) const
{
cout << "OverrideMe from T" << a << endl;
}
};
class Derived: public T<int>
{
public:
virtual void OverrideMe( int da )
{
cout << "OverrideMe from Derived" << da << endl;
}
};
int main( void ) {
T<int> *p = new Derived();
Derived q;
T<int> r( q );
p->CallMe( 4 );
q.CallMe( 5 );
r.CallMe( 6 );
return 0;
}
--
. . . . . . . . . . . . . . . . . . . . . . . . . . .
Microsoft is not the answer. > Julian Harris, Programmer >
Microsoft is the question. > Comp. Sci. Dept. x8915 >
> The University of Auckland >
NO is the answer. > julian@cs.auckland.ac.nz >
Technical Details
Message-ID:
<julian-1810951406510001@julian.cs.auckland.ac.nz>Path:
arc1-ca.pugleaf.net!julian.cs.auckland.ac.nz!userShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!julian.cs.auckland.ac.nz!user From: julian@cs.auckland.ac.nz (Julian Harris) Newsgroups: comp.sys.mac.programmer.codewarrior,comp.lang.c++ Subject: C++ comment on warnings about overloaded functions (Bug?) Date: Wed, 18 Oct 1995 14:06:51 +1300 Organization: Computer Science, The University of Auckland Message-ID: <julian-1810951406510001@julian.cs.auckland.ac.nz> NNTP-Posting-Host: julian.cs.auckland.ac.nz X-Newsreader: Yet Another NewsWatcher 2.0