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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:27:41+00:00 2026-06-06T23:27:41+00:00

I have two class that inherit the same abstract base class: class base {

  • 0

I have two class that inherit the same abstract base class:

class base
{ virtual void something() = 0; };

class a : public base
{
     void something();
};

class b : public base
{
     void something();

     // This is what I want: a list where I can store values of type a
     // and of type b
     // std::list<a & b> objs;
};

I could use a list of raw/smart pointers (list<base*> obj_ptrs), but how to use this list?

b b_obj;
b_obj.obj_ptrs.push_back(new a());

// Who have to delete this pointer? Who use the class or who design
// the class in the object destructor?

// The following is valid for c++11
auto p = b_obj.obj_ptrs.back();
// But if i'm using c++03?

I want that who use the class have the possibility to do this:

a a_obj;
b b_obj;

b_obj.obj_ptrs.push_back(a);
b_obj.obj_ptrs.push_back(b);

How have I to design my classes to do this work?

  • 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-06T23:27:43+00:00Added an answer on June 6, 2026 at 11:27 pm

    I could use a list of raw/smart pointers (list<base*> obj_ptrs), but how to use this list?

    You would interact with them polymorphically by calling virtual functions or, in exceptional circumstances, using dynamic_cast to convert the pointer type to a* or b*.

    Who have to delete this pointer?

    You will have to decide on the ownership scheme. One approach is to store smart pointers (usually unique_ptr, or perhaps shared_ptr in special circumstances) in the list, and then they’ll be automatically deleted on removal from the list. Another approach is to manage the objects separately from the list, and just put raw pointers in the list.

    // The following is valid for c++11
    auto p = b_obj.obj_ptrs.back();
    // But if i'm using c++03?
    

    That’s equivalent to base * p = b_obj.obj_ptrs.back();. auto will not magically give the dynamic pointer type, it’s just a shortcut for the static pointer type.

    How have I to design my classes to do this work? (storing objects of different types)

    Storing objects of different types can be done using a discriminated union – basically, a union with some extra metadata to keep track of which union member is active. These are quite tricky to implement in C++, but you could use Boost.Variant or Boost.Any. This has the advantage that you don’t need a common base class, or any other support from the types being stored; any set of (non-abstract) types can be used.

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

Sidebar

Related Questions

So let's say I have two classes that inherit a base class that has
I have an abstract base class Contact that has two subclasses: Person and Company
I have a bas class called Media with two classes that inherit from it,
I have a class that contains two sets. They both contain the same key
I have a class that instantiates two classes which implement interfaces. I want one
I have 2 classes that inherit UITableViewControllers. And this two table views should use
I have two classes(Book,Software) that inherit from the Product class. these two classes have
I have an abstract base class which acts as an interface. I have two
I have two lists based on the same class. I would like one list
I have a Runnable class that I'm writing. Inside of it, I have two

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.