Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:33:55+00:00 2026-06-06T22:33:55+00:00

Basically from C++ FAQ I learned that: A virtual function allows derived classes to

  • 0

Basically from C++ FAQ I learned that:

A virtual function allows derived classes to replace the
implementation provided by the base class. The compiler makes sure the
replacement is always called whenever the object in question is
actually of the derived class, even if the object is accessed by a
base pointer rather than a derived pointer. This allows algorithms in
the base class to be replaced in the derived class, even if users
don’t know about the derived class.

Though, using templates this becomes no more correct.

I need to have a template class which is derived by another template class. After that I found it makes no sense to have a virtual function in a base template class, googling around I found that there is a technique to overcome limitations of polymorphism in generic programming called Type Erasure.

Examples and tutorials I’ve read, are almost irrelevant to my needs. Also I’m not sure if I understood concepts of Type Erasure correctly. Being a beginner, I need to simply do:

template <typename T>
class Foo
{
public:
    virtual void doX(){cout<<"doX from Foo"<<endl;}
};

template <typename T>
class Bar : public Foo<T>
{
public:
    void doX(){cout<<"doX from Bar"<<endl;}
};

And,

Foo<int>* p;
Foo<int> i;
i.doX();           // "doX from Foo", it's ok
Bar<int> j;
j.doX();           // "doX from Bar", it's ok
p=&i;
p->doX();          // should be "doX from Foo", it's ok
p=&j;
p->doX();          // I expect "doX from Bar", but it's "doX from Foo"

EDIT:
My question is how to implement above behavior? Without templates, It works as expected. I need same for general classes.

Simply, I have a base class B. It’s a template class. It has a virtual function. And also I have a subclass of B which is a template class too, and it should reimplement some behavior of B (somehow implement virtual function of B).

EDIT2 : Just edited the title.
I was compiling my code with g++ 4.6.3 compiler using flag -std=c++0x because I need std::thread and std::unique_ptr in my code. Surprisingly, removing the -std=c++0x the above code works as expected. I also tried g++ 4.7 and there was no problem with both 2011 and 2003 standards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:33:57+00:00Added an answer on June 6, 2026 at 10:33 pm

    Because function doX is not virtual. In derived class you hide function doX, not overload, so dynamic polymorphysm not work.

    EDIT.
    GCC 4.6.3 have bug in such case, when you use -std=c++0x flag.
    EDIT2.
    Possibly GCC 4.6.3 have no such bug and problem is with TS compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a conversion utility that basically copies values from one table to another.
I'm making a Android app that basically plays video from a website on the
Basically I pull an Id from table1, use that id to find a site
Basically from a database I am getting data that is formatted like this nameofproject101
i have something here that is basically from jqueryui. Yes, it is raw and
I have a QTableWidget that is used to display lots of data, basically derived
I've got a program made basically from a ListCtrl and has an add button,
Basically we moved from IIS 5 to IIS 7 and I am trying to
Can I have some help in improving this linq. I'm basically returning speakers from
I am having a problem getting this code to work. Basically I Go from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.