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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:31:10+00:00 2026-05-22T01:31:10+00:00

I’m not very experienced with C++ yet, so bear with me if this is

  • 0

I’m not very experienced with C++ yet, so bear with me if this is basic stuff.

I have some code like that below. L is an abstract class (it has a number of pure virtual functions), and A, B and C are regular classes all derived from L. There may be any number of these, and they are all different.

int main() {
    // ...

    std::vector<L*> ls(3) ; 

    ls[0] = new A ;
    ls[1] = new B ;
    ls[2] = new C ;

    int i ;
    for (i = 0 ; i < ls.size() ; i++) {
        if (ls[i]->h()) {
            // ...
        }
    }

    // ...
}

It works, but there really has to be a better way to initialise that vector. Right?

The vector is not supposed to change after it has been first initialised. I figure I can’t make it const, however, because the various objects may themselves change internally. I picked a vector over a regular array because I don’t want to manually keep track of its length (that proved error prone).

Ideally I’d like to pull the definition and initialisation of the vector out of main and preferably into a separate file that I can then #include. When I try that the compiler complains that it “expected constructor, destructor, or type conversion before ‘=’ token”. All the classes A, B and C have default constructors.

Also, I was under the impression that I have to manually delete anything created using new, but it won’t delete ls with either delete or delete[]. If I try delete ls; the compiler complains that “type ‘class std::vector<L*, std::allocator<L*> >’ argument given to ‘delete’, expected pointer”.

Is the above even safe or does it cause some memory problems?

  • 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-22T01:31:11+00:00Added an answer on May 22, 2026 at 1:31 am

    but there really has to be a better way to initialise that vector. Right?

    I don’t think so, at least not without C++0x. Which way would you prefer? Your initialization code is completely fine.

    I figure I can’t make it const, however, because the various objects may themselves change internally.

    You can still make the vector itself const, only its member type cannot be a pointer to const then.

    I picked a vector over a regular array because I don’t want to manually keep track of its length (that proved error prone).

    You don’t have to keep track of the length in constant arrays:

    L* ls[] = { new A, new B, new C };
    // with <boost/range/size.hpp>
    std::size_t num = boost::size(ls);
    // without Boost, more error-prone
    // std::size_t num = sizeof ls / sizeof ls[0];
    

    And often you don’t need the size anyway, e.g. with Boost.Range.

    Ideally I’d like to pull the definition and initialisation of the vector out of main and preferably into a separate file that I can then #include.

    That would violate the one-definition rule. You can put the declaration into a header file, but the definition has to go into a source file.

    Also, I was under the impression that I have to manually delete anything created using new, but it won’t delete ls with either delete or delete[].

    Your impression is correct, but you haven’t created ls with new, only its elements. After using the vectors, you have to delete each of its elements, but not the vector itself.

    The recommended alternative to STL containers holding polymorphic pointers is the Boost pointer container library.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.