473,387 Members | 1,483 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,387 software developers and data experts.

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 Tclass TypedInterface
{
public:
virtual void TestFunction( T * ) = 0;
};
template<typename Tclass TypedInterfaceImplementation :
public TypedInterface<T>
{
public:
void TestFunction( T * ) { };
};
template<typename Tclass 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
Nov 7 '07 #1
5 1856
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.
Nov 7 '07 #2
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 Tclass Test :
public TypedInterfaceImplementation< T >
{
public:
void TestFunction( T , T ) { };
using TypedInterfaceImplementation<T>::TestFunction;
};

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

Michael
Nov 7 '07 #3
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

Nov 7 '07 #4

"Michael DOUBEZ" <mi************@free.frwrote in message
news:47***********************@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 Tclass 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.
Nov 7 '07 #5
Hello again,

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

using TypedInterfaceImplementation<T>::TestFunction;

it works!

Lars
Nov 8 '07 #6

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

Similar topics

4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
3
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hi, I am posting this again, because my previous message dated 18.09. disappeared from the list (someone has cancelled it ? why ??). I have a problem with compiling the...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
4
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although...
4
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which...
4
by: Przemyslaw Koprowski | last post by:
Hi, I have the following problem. Consider two simple classes AA and BB, BB inherits from AA. AA contains a class A inside and a pure virtual method getA returning an object of class A. BB...
4
by: harsh.murari | last post by:
In the example below, a single object (Network) is implementing 2 interfaces (INetworkA and INetworkB). Both of these interfaces derive from the IBase interface (similar to the IUnknown interface...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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
0
BarryA
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...
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
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.