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

Overwritten method in different class interfaces


Hello

I'm not really sure if this should be possible according to the
standard, because didn't found anything in the FAQ and in the C++ book
about that.

==== example ===
class A
{
public:
virtual void foo( int x = 0 ) = 0;
};

class B : public A
{
public:
virtual void foo( double x = 0.0 ) = 0;
};

class Impl : public B
{
public:
virtual void foo( int x ) {};
virtual void foo( double x ) {};
};

int main( int argc, char* argv[] )
{
B* b = new Impl();
int x;
b->foo();
}
==== example ===

I have two interfaces with pure virtual methods. But the methods of the
different interfaces are related together, because the B interface
overloading the method from the interface A. And with the default
arguments, it should be a conflict.

best regards
Tobias
Dec 13 '06 #1
2 1759

Tobias Erbsland wrote:
Hello

I'm not really sure if this should be possible according to the
standard, because didn't found anything in the FAQ and in the C++ book
about that.
http://www.parashift.com/c++-faq-lit....html#faq-23.9

I don't know if your compiler gave that warning or not - for me, Comeau
online did and MSVC++8 did not - but that's what your code does.
>
==== example ===
class A
{
public:
virtual void foo( int x = 0 ) = 0;
};

class B : public A
{
public:
virtual void foo( double x = 0.0 ) = 0;
This does not overload foo in A, it *hides* it. To call the function
inherited from A, you either need a using declaration in class B (see
the FAQ) or you need to qualify the function name when you call it (see
below).
};

class Impl : public B
{
public:
virtual void foo( int x ) {};
virtual void foo( double x ) {};
};

int main( int argc, char* argv[] )
{
B* b = new Impl();
int x;
b->foo();
}
==== example ===

I have two interfaces with pure virtual methods. But the methods of the
different interfaces are related together, because the B interface
overloading the method from the interface A. And with the default
arguments, it should be a conflict.
Do you mean you were expecting a compile error? I tweaked your program
to illustrate what's happening.

#include <iostream>
using namespace std;

class A
{
public:
virtual void foo( int x = 0 ) = 0;
};

class B : public A
{
public:
virtual void foo( double x = 0.0 ) = 0;
};

class Impl : public B
{
public:
virtual void foo( int x )
{
cout << "Impl::foo(int) with value " << x << "\n";
};
virtual void foo( double x )
{
cout << "Impl::foo(double) with value " << x << "\n";
};
};

int main()
{
B* b = new Impl;
b->foo();

A* a = new Impl;
a->foo();
}

That should compile and run, and output

Impl::foo(double) with value 0
Impl::foo(int) with value 0

Gavin Deane

Dec 13 '06 #2
"Tobias Erbsland" <ne***************@tracker.drzoom.chwrote in message
news:45**********************@news.sunrise.ch...
: I'm not really sure if this should be possible according to the
: standard, because didn't found anything in the FAQ and in the C++ book
: about that.
:
: ==== example ===
: class A
: {
: public:
: virtual void foo( int x = 0 ) = 0;
: };
:
: class B : public A
: {
: public:
: virtual void foo( double x = 0.0 ) = 0;
: };
:
: class Impl : public B
: {
: public:
: virtual void foo( int x ) {};
: virtual void foo( double x ) {};
: };
:
: int main( int argc, char* argv[] )
: {
: B* b = new Impl();
: int x;
: b->foo();
: }
: ==== example ===
:
: I have two interfaces with pure virtual methods. But the methods of
the
: different interfaces are related together, because the B interface
: overloading the method from the interface A. And with the default
: arguments, it should be a conflict.

I am afraid that this would (unfortunately) be legal,
and will call B::foo(double 0).

Seen from an instance of Impl, A's methods are in a scope that
is "further out" than B's methods.
In other words, "A::foo(int)" is hidden by "B::foo(double)".

I have no further details to give. The thing is, it is a Bad Idea(TM)
to mix virtual functions with overloading *or* default parameters
( and that, you will find confirmed in books and FAQs ;) ), because
it can lead to unexpected changes of the meaning of your code.
There is no reason to get into this minefield.

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Dec 13 '06 #3

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
3
by: bauscharln | last post by:
hoi, I just wanted to add a static method to my interface, but that's not allowed! Is there a reason for this??? (Yes, I could make an abstract base class, but that's not what I want, since I...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
2
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
2
by: macracan | last post by:
It has been discussed before, but I still can't find a solution and I have a need. So here goes: The problem: I'm writing a something to handle messages from XWindows. The idea is to have...
2
by: Tony | last post by:
Hello! According to the documentation we have the following interface IList : ICollection, IEnumerable { I can understand all the methods in this IList } interface ICollection : IEnumerable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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
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.