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 7058969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:10:34+00:00 2026-05-28T04:10:34+00:00

There are two classes A and B, where A is the base class and

  • 0

There are two classes A and B, where A is the base class and B is the derived class:

template <typename T>
class A {T a;};

template <typename T>
class B: public A <T> {T b;};

and the following class representing a modified container

template <typename Item>
struct TItems {typedef std::vector <Item> Type;};

template <typename Item>
class ModCont
{
    private:
            typename TItems <Item>::Type items;
};

A function test() has pointer to container of A objects as formal parameter:

template <typename T>
void test ( ModCont <A <T> > *it) {}

I would like to apply polymorphism and pass container of B to the method test:

int main(int argc, char* argv[])
{

  ModCont < A <double> > a_items;
  ModCont < B <double> > b_items;
  test (&a_items); //Works
  test (&b_items); //Does not work
  return 0;
}

The only way I found is to templatize a parameter of the test() method in this way:

template <typename Object>
void test ( ModCont <Object> *it) {}

Is there any way how to use the the “function” polymorphism instead of the compile polymorphism (templates?)

Thanks for your help…

  • 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-05-28T04:10:35+00:00Added an answer on May 28, 2026 at 4:10 am

    Uhm, first of all, templates are not runtime polymorphism — they’re compile-time polymorphism.

    If you want to use runtime polymorphism with this, you have to make sure that ModCont<B<T> > derives from ModCont<A<T> > — the way C++ handles polymorphism does not make this the default. Alternatively, you could have all ModCont<T> derive from some general ModContBase, although it’s unclear how that would work.

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

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
I have a base class Element and derived two other classes from it, Solid
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
If I have a base class and two derived classes, and I want to
I have a base class, Parameter, and two derived classes: Scalar & Vector. In
I have these two classes public class Person { } public class Company {
This is easier to explain with an example. Given these two classes: public class
Say we have a class inheriting from two base classes (multiple inheritance). Base class
Is there a way to transform a base class into its derived class? Here

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.