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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:50:26+00:00 2026-06-17T05:50:26+00:00

In a simplistic design, a class B inherits a class A polymorphically. A templated

  • 0

In a simplistic design, a class B inherits a class A polymorphically.
A templated class, Base<T> has a T* member that is used for further operations. A Derived<T> inherits from Base<T> polymorphically.
What would be the syntax that allows this kind of object creation:

Base<A>* a = new Derived<B>();

For further reference, the code I used looks like this (of course, the conversion does not succeed):

class A
{
public:
  A()
  {
    cout<< " A  ";
  }
  virtual void one()
  {
    cout<<" 1 ";
  }

};

class B: public A
{
public:
  B()
  {
    cout << " B  ";
  }
  void one()

  {
      cout << " 2 ";
  }
};

template <class T>
class Base
{

public:
    T* thing;
  Base()
  {
    cout<<"Base";
    thing = new T;
  }
  template <class S>
  Base(Base<S>* obj)
  {
    thing = obj->thing;
  }
  virtual void poly(){ thing->one();}
};
template <class T>
class Derived : public Base<T>
{
public:
  Derived()
  {
    cout << "DERIVED ";
  }
  virtual void poly(){ 
};
int main(int argc, char** argv)
{
  //Base<A>* a = (new Derived<B>());
  return 0;
}

Virtual destructors and proper memory management omitted on purpose for code brevity.

EDIT : the sole purpose of this construction would be to keep, say, a list of BaseTemplated<BasePolymorphic>* pointers together, and not use BaseTemplated<Derived1> to BaseTemplated<DerivedN> for all N subclasses of a base, polymorphic class.

  • 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-17T05:50:27+00:00Added an answer on June 17, 2026 at 5:50 am

    First, tell me when we get to the “simplistic” part.

    Peel back all half of the templates to just concentrate on the part you’re trying to polymorph). Eventually you have distinct classes Base<A> and Base<B>. (the latter through derivation of Derived<B>).

    Neither of these inherit from their respective template parameters. Therefore the relationship (hierarchical or otherwise) of A to B is irrelevant. Base<A> and Base<B> are distinct and unrelated, and therefore what you’re trying to do as-writen cannot work. In fact, even if the did inherit from a A and B respectively, the very best you could hope for is an A* pointer, which you aren’t using in your sample.

    And I’ll gladly delete this if shown otherwise, because I’m genuinely curious.

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

Sidebar

Related Questions

I'm writing a simplistic HTTP server that will accept PUT requests mostly from cURL
I have a simplistic app (just learning) that reads some XML data from a
I wrote a simplistic DBResourceMonitor class which is used by a set of database
This article claims that each register has an intended purpose and more importantly, When
These are fairly simplistic questions, but something that I wanted to get right in
I have a method that performs a simplistic 'grep' across files, using an enumerable
Which one is better and cleaner from an implementation stand point so that one
I have a very simplistic user control that looks like this: <%@ Control Language=C#
I have a rather simplistic example that I would like to learn the best
I'm busy creating a very simplistic ruby on rails app that won't need a

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.