Overwritten method in different class interfaces

T

Tobias Erbsland

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
 
G

Gavin Deane

Tobias said:
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-lite/strange-inheritance.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
 
I

Ivan Vecerina

: 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top