Weird behaviour with templates, virtual inheritance and overloadedmethods



 DEVELOP > c-Plus-Plus > Weird behaviour with templates, virtual inheritance and overloadedmethods

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Lars Hillebrand"
Date: 07 Nov 2007 05:47:02 AM
Object: Weird behaviour with templates, virtual inheritance and overloadedmethods
Hello,
i discovered a weird behaviour if i use templates together with virtual
inheritance and method over. I managed to reproduce my problem with a
small example:
// *********** <code example> **********
template<typename T> class TypedInterface
{
public:
virtual void TestFunction( T * ) = 0;
};
template<typename T> class TypedInterfaceImplementation :
public TypedInterface<T>
{
public:
void TestFunction( T * ) { };
};
template<typename T> class Test :
public TypedInterfaceImplementation< T >
{
public:
void TestFunction( T , T ) { };
};
void testme()
{
Test< float > TestObject;
// Call to Test::TestFunction, OK:
TestObject.TestFunction( 1.0, 1.0 );
float fOne = 1.0;
// Call to TypedInterfaceImplementation::TestFunction results in an
error:
// error C2660: 'Test<T>::TestFunction': function does not take 1
parameters
TestObject.TestFunction( &fOne );
}
// *********** </code example> **********
The method 'TypedInterfaceImplementation::TestFunction' doesn't seem to
belong to the interface of class 'Test', but it should be available
because i only use public inheritance. The method
'Test<T>::TestFunction( T , T )' should just overload the virtual
method, not overwrite it!
If i rename 'Test<T>::TestFunction( T , T ) { };' so it is not
overloaded anymore, the error is gone. But i would realy like to
overload it.
Am i doing something wrong?
I'm using Visual Studio 2005 (i have to :-( ), Version 8.0.50727.42, no
service-pack.
Thanks in advance for all answers!
Lars
.

User: "Duane Hebert"

Title: Re: Weird behaviour with templates, virtual inheritance and overloaded methods 07 Nov 2007 06:45:47 AM

The method 'TypedInterfaceImplementation::TestFunction' doesn't seem to
belong to the interface of class 'Test', but it should be available
because i only use public inheritance. The method
'Test<T>::TestFunction( T , T )' should just overload the virtual method,
not overwrite it!
If i rename 'Test<T>::TestFunction( T , T ) { };' so it is not overloaded
anymore, the error is gone. But i would realy like to overload it.

Am i doing something wrong?

I'm not positive with templates, but without templates,
you would be hiding the base function, not overriding
or overloading it.
One way around it is to bring the base function into
scope with a using clause but I'm not sure how this
works with templates.
.
User: "Michael DOUBEZ"

Title: Re: Weird behaviour with templates, virtual inheritance and overloadedmethods 07 Nov 2007 07:04:00 AM
Duane Hebert a écrit :

The method 'TypedInterfaceImplementation::TestFunction' doesn't seem to
belong to the interface of class 'Test', but it should be available
because i only use public inheritance. The method
'Test<T>::TestFunction( T , T )' should just overload the virtual method,
not overwrite it!
If i rename 'Test<T>::TestFunction( T , T ) { };' so it is not overloaded
anymore, the error is gone. But i would realy like to overload it.

Am i doing something wrong?


I'm not positive with templates, but without templates,
you would be hiding the base function, not overriding
or overloading it.

One way around it is to bring the base function into
scope with a using clause but I'm not sure how this
works with templates.

That way:
template<typename T> class Test :
public TypedInterfaceImplementation< T >
{
public:
void TestFunction( T , T ) { };
using TypedInterfaceImplementation<T>::TestFunction;
};
Otherwise, TypedInterfaceImplementation<T>::TestFunction(T*) never get
instanciated.
Michael
.
User: "Duane Hebert"

Title: Re: Weird behaviour with templates, virtual inheritance and overloaded methods 07 Nov 2007 05:32:27 PM
"Michael DOUBEZ" <michael.doubez@free.fr> wrote in message
news:4731b5af$0$15440$426a34cc@news.free.fr...

Duane Hebert a écrit :

The method 'TypedInterfaceImplementation::TestFunction' doesn't seem to
belong to the interface of class 'Test', but it should be available
because i only use public inheritance. The method
'Test<T>::TestFunction( T , T )' should just overload the virtual
method, not overwrite it!
If i rename 'Test<T>::TestFunction( T , T ) { };' so it is not
overloaded anymore, the error is gone. But i would realy like to
overload it.

Am i doing something wrong?


I'm not positive with templates, but without templates,
you would be hiding the base function, not overriding
or overloading it.

One way around it is to bring the base function into
scope with a using clause but I'm not sure how this
works with templates.


That way:

template<typename T> class Test :
public TypedInterfaceImplementation< T >
{
public:
void TestFunction( T , T ) { };
using TypedInterfaceImplementation<T>::TestFunction;
};

Otherwise, TypedInterfaceImplementation<T>::TestFunction(T*) never get
instanciated.

That's what I expected though I hadn't had a
chance to test it. It's just a case of "hiding" and
nothing to do with templates. Same would be true
of a straight class hierarchy.
.



User: "Andrey Tarasevich"

Title: Re: Weird behaviour with templates, virtual inheritance and overloadedmethods 07 Nov 2007 01:40:48 PM
Lars Hillebrand wrote:

i discovered a weird behaviour if i use templates together with virtual
inheritance and method over.

The behavior you "discovered" is called name hiding. It really has
nothing to do with templates, virtual inheritance and such. (And BTW
there's no virtual inheritance in your code.) A much simpler example
that demonstrates the same error could look as follows
struct A { void foo(int) {} };
struct B : A { void foo(int, int) {} };
int main() {
B b;
b.foo(5); // ERROR
}
Read the FAQ on name hiding to understand why it fails.
--
Best regards,
Andrey Tarasevich
.
User: "Lars Hillebrand"

Title: Re: Weird behaviour with templates, virtual inheritance and overloadedmethods 08 Nov 2007 02:04:43 AM
Hello again,
thanks for all answers!
With a 'using' clause like this:
using TypedInterfaceImplementation<T>::TestFunction;
it works!
Lars
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER