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

  • Home
  • SEARCH
  • 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 8439579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:01:59+00:00 2026-06-10T08:01:59+00:00

I have a base structure FooBase : struct FooBase { }; Then I create

  • 0

I have a base structure FooBase:

struct FooBase { };

Then I create a template structure Foo which is a child of FooBase:

template <typename typeName> struct Foo : public FooBase { typeName* foo };

In some class I create a vector of FooBase and add instances of Foo in it:

vector <FooBase> FooVector
...    
Foo <Bar> fooInstance;
fooInstance.foo = new Bar();
FooVector.push_back ( fooInstance );

Then I needed to access the stored data, but I’m getting predictable and obvious error about an absence of the member foo in FooBase

FooVector[0].foo

I can not write something like

Foo <Bar> fooInstance = FooVector[0]

since I don’t know the template parameter.

How do I store instances of Foo in the vector so I can access them later. Note, that I don’t know the template parameter at the last step – when reading data from the vector.

P.S. NO BOOST ALLOWED!

  • 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-10T08:02:02+00:00Added an answer on June 10, 2026 at 8:02 am

    What happens here, is that in your

    FooVector.push_back ( fooInstance );
    

    line, C++ silently invokes the copy constructor of FooBase, because you can only keep objects of that type in your vector. Since Foo inherits publicly from FooBase the method FooBase::FooBase(FooBase const&) can be called with an object of type Foo.

    So, you’re not really storing Foos, but in fact FooBases. To do what you want to do you need an std::vector<FooBase*> or std::vector<std::shared_ptr<FooBase> >.

    However, the contents of your vector, will still lack a foo member, because the static type is still not Foo. To get around this, you have some options. You could dynamic_cast or static_cast your FooBase* into a Foo* and then access its foo member. But that could break, since the FooBase* pointers might actually hold another type than Foo.

    Why don’t you just use an std::vector<Foo<Bar> > instead?

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

Sidebar

Related Questions

I have a base DLL which defines some basic structure and operation for a
I have the following structure: class Base { } class Child : Base {
I have an app (cms) which structure is like this: BASE: /application/ /public/ themes/
I have the following structure: abstract class Base { public abstract List<...> Get(); //What
I have a structure of base class and a couple of inherited classed. Base
I have an entity model where the base class in an inheritance structure has
I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>
in my zf application i have base model class Application_Model, which connects to db
I have a base class: class motorcycle { public: virtual int speed() { return
I have some base layout with site structure. In this default layout I define

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.