Hi,
just wondering if there is something in the C++ language standard that
forbids the following construct:
class Base {
public:
virtual int GetSomething( int x, int y );
int GetSomething( double z );
};
#include "Base.h"
class Derived: public Base {
public:
virtual int GetSomething( int x, int y );
};
.....
#include "Derived.h"
{
Derived *obj = new Derived();
obj->GetSomething( 2.0 );
}
Neither gcc nor Microsoft's compiler liked this, indicating that the
double 2.0 could not be cast to an int or that there is no matching
function. Is this forbidden in the standard or are the compilers not
meeting standard?
A workaround is to declare GetSomething( double ) as virtual also, but
because this method has no need to be virtual, each derived method just
calls the base method. Another alternative is to rename the method. In
any case, it seems unnecessary except that the compilers refuse to work
with the code as written. 3 1417
On Sat, 18 Jun 2005 22:41:39 +0400, Joe P <jp@nospam.invalid> wrote:
[] Neither gcc nor Microsoft's compiler liked this, indicating that the double 2.0 could not be cast to an int or that there is no matching function. Is this forbidden in the standard or are the compilers not meeting standard?
Please refer to http://new-brunswick.net/workshop/c+....html#faq-23.6
--
Maxim Yegorushkin
Joe P wrote: Hi, just wondering if there is something in the C++ language standard that forbids the following construct:
class Base { public: virtual int GetSomething( int x, int y ); int GetSomething( double z ); };
#include "Base.h" class Derived: public Base { public: virtual int GetSomething( int x, int y ); };
.... #include "Derived.h" { Derived *obj = new Derived(); obj->GetSomething( 2.0 ); }
Neither gcc nor Microsoft's compiler liked this, indicating that the double 2.0 could not be cast to an int or that there is no matching function. Is this forbidden in the standard or are the compilers not meeting standard?
The compilers are applying the standard correctly. Because you have
defined a GetSomething function in Derived, it masks any other
GetSomething functions from parent classes. If you want to use one of
the masked functions, you can use the using keyword or use is by
specifying the scope.
class Base {
public:
virtual int GetSomething( int x, int y ) { return 0; }
int GetSomething( double z ) { return 0; }
};
class Derived: public Base {
public:
using Base::GetSomething;
virtual int GetSomething( int x, int y ) { return 1; }
};
class Derived1: public Base {
public:
virtual int GetSomething( int x, int y ) { return 1; }
};
int main()
{
Derived *obj = new Derived();
obj->GetSomething( 2.0 );
Derived1 *obj1 = new Derived1();
obj1->Base::GetSomething( 2.0 );
obj1->GetSomething( 2.0 ); // error
} A workaround is to declare GetSomething( double ) as virtual also, but because this method has no need to be virtual, each derived method just calls the base method. Another alternative is to rename the method. In any case, it seems unnecessary except that the compilers refuse to work with the code as written.
You could also simply write a wrapper.
class Derived: public Base {
public:
virtual int GetSomething( int x, int y ) { return 1; }
int GetSomething( double z ) { return Base::GetSomething( z ); }
};
On Sat, 18 Jun 2005 18:30:24 -0700, Gianni Mariani wrote: You could also simply write a wrapper.
class Derived: public Base { public: virtual int GetSomething( int x, int y ) { return 1; } int GetSomething( double z ) { return Base::GetSomething( z ); } };
Thanks, this seems the cleanest solution. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. ...
|
by: Stub |
last post by:
When I try to overload the == operator, it gives me an "error C2804: binary
'operator ==' has too many parameters."
bool operator==(const Store& Store1, const Store& Store2);
After Adding...
|
by: matthias_k |
last post by:
Hi,
I've never thought about this before, but since you are able to overload
a function only by changing the const-ness of the formal parameters,
some annoying side effects will arise.
For...
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
|
by: KL |
last post by:
I am working on a school assignment, so please don't tell me the
solution. I just want some direction.
I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having...
|
by: siddhu |
last post by:
can we overload functions in a following way?
class Fred { ... };
class MyFredList {
public:
const Fred& operator (unsigned index) const;
Fred& operator (unsigned index);
};
|
by: luca regini |
last post by:
I have this code
class M
{
.....
T operator()( size_t x, size_t y ) const
{ ... Operator overloading A ....}
T& operator()( size_t x, size_t y )
|
by: Jerry Fleming |
last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), ,...
|
by: jakester |
last post by:
I am using Visual C++ 2007 to build the code below. I keep getting
linkage error. Could someone please tell me what I am doing wrong? The
code works until I start using namespace for my objects.
...
|
by: subramanian100in |
last post by:
The following is a beginner's question.
Suppose TYPE1 and TYPE2 are two types for which suitable ctors and
operator= are defined.
Suppose I have
class Test
{
TYPE1 mem1;
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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: 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...
| |