Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Weird behaviour with templates, virtual inheritance and overloadedmethods

Reply
Thread Tools

Weird behaviour with templates, virtual inheritance and overloadedmethods

 
 
Lars Hillebrand
Guest
Posts: n/a
 
      11-07-2007
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
 
Reply With Quote
 
 
 
 
Duane Hebert
Guest
Posts: n/a
 
      11-07-2007
> 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.


 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      11-07-2007
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-07-2007
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

 
Reply With Quote
 
Duane Hebert
Guest
Posts: n/a
 
      11-07-2007

"Michael DOUBEZ" <> wrote in message
news:4731b5af$0$15440$...
> 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.


 
Reply With Quote
 
Lars Hillebrand
Guest
Posts: n/a
 
      11-08-2007
Hello again,

thanks for all answers!
With a 'using' clause like this:

using TypedInterfaceImplementation<T>::TestFunction;

it works!

Lars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't. pauldepstein@att.net C++ 1 03-14-2009 03:45 PM
virtual inheritance and virtual function. Ashwin C++ 2 08-01-2006 12:48 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments