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

Polymorphism via Inheritance not working as planned

joe
I expected this to work:

class Base
{
virtual void go(int *) {cout<<"BASE CLASS"<<endl;}
};

class Derived : public Base
{
template<typename T>
void go(T*) {cout<<"Derived Class"<<endl;}

};

int main(){
Base *b = new Derived;
b->go(new int[30]);
}

I get "BASE CLASS". I expected the virtual function to work and find
the derived class function.

What's the nitty gritty here of what's going on?

May 23 '06 #1
5 1476
joe wrote:
I expected this to work:
On what grounds?
class Base
{
virtual void go(int *) {cout<<"BASE CLASS"<<endl;}
};

class Derived : public Base
{
template<typename T>
void go(T*) {cout<<"Derived Class"<<endl;}
Here is a possible problem: a member template cannot be virtual.
So, even if 'T' *is* 'int', you're not going to achieve making the
instantiation of that template the final overrider to 'Base::go'.

};

int main(){
Base *b = new Derived;
b->go(new int[30]);
}

I get "BASE CLASS". I expected the virtual function to work and find
the derived class function.
Why? The derived class' member is not the overrider for the base's one.

What's the nitty gritty here of what's going on?


When you call 'go', how should the compiler know you're trying to call
the template? If you were allowed to make 'Derived::go' virtual, how
many virtual functions would 'Derived' have?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 23 '06 #2

joe wrote:
I expected this to work:

class Base
{
virtual void go(int *) {cout<<"BASE CLASS"<<endl;}
};

class Derived : public Base
{
template<typename T>
void go(T*) {cout<<"Derived Class"<<endl;}

};

int main(){
Base *b = new Derived;
b->go(new int[30]);
}

I get "BASE CLASS". I expected the virtual function to work and find
the derived class function.

What's the nitty gritty here of what's going on?


Name resolution is performed statically (during compile time).
Derived::go does not override Base::go, it hides it (actually in this
case you might get a compilation error if it is ever instantiated for
int*). However, since you are calling through a pointer to Base,
Base::go is in scope and is resolved. As there is no Derived::go that
overrides Base::go the pointer in the table is to Derived::go and that
is what is called. No instance of the Derived::go template function is
created as it is never called; such instantiation can only occur
statically and cannot be virtual.

May 23 '06 #3

Noah Roberts wrote:
Derived::go does not override Base::go, it hides it (actually in this
case you might get a compilation error if it is ever instantiated for
int*).


At least on VC++ 7 it hides the definition of Base::go in Derived even
when the template type is exactly the same as the virtual function.
When you call go through a Derived pointer it calls Derived::go<int>,
when you call from a Base pointer it calls Base::go. This might very
well be undefined behavior though and so it working doesn't really mean
much unless someone more familiar with the standard can say what is
what.

May 23 '06 #4
Noah Roberts wrote:
Noah Roberts wrote:
Derived::go does not override Base::go, it hides it (actually in this
case you might get a compilation error if it is ever instantiated for
int*).


At least on VC++ 7 it hides the definition of Base::go in Derived even
when the template type is exactly the same as the virtual function.
When you call go through a Derived pointer it calls Derived::go<int>,
when you call from a Base pointer it calls Base::go. This might very
well be undefined behavior though and so it working doesn't really
mean much unless someone more familiar with the standard can say what
is what.


It's not undefined. 14.5.2/4 simply says that a specialisation of
a member function template does not override a virtual function from
a base class. So, if it doesn't override, it must be hiding it.
You can bring in the Base::go (by declaring it), and then they both
will coexist peacefully, and to call the template for a Derived object
you'd need to supply the template argument list.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 23 '06 #5

joe wrote:
I expected this to work:

class Base
{
virtual void go(int *) {cout<<"BASE CLASS"<<endl;}
};

class Derived : public Base
{
template<typename T>
void go(T*) {cout<<"Derived Class"<<endl;}

};

int main(){
Base *b = new Derived;
b->go(new int[30]);
}

I get "BASE CLASS". I expected the virtual function to work and find
the derived class function.

What's the nitty gritty here of what's going on?


Now, on the other hand, the following will do what you expect:

template<typename T>
class Derived : public Base
{
void go(T*) { cout << "DERIVED" << endl; }
};

int main()
{
Base * b = new Derived<int>;
b->go();
}

This is because class templates are a different story. Instantiation
also links up virtual functions to implementations in the derived class.

May 23 '06 #6

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

Similar topics

13
by: Gurtaj | last post by:
Hello! I am working with PHP and MySQL in a proyect with some partners of our university, and we are thinking about building some classes and use inheritance and polymorphism. The problem is...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
10
by: Lino Barreca | last post by:
Take a look at this code: Class clsAnagrafica Public Overridable ReadOnly Property Codice() As Integer Get Return 1 End Get End Property End Class
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
8
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.