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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:20:19+00:00 2026-06-12T23:20:19+00:00

I wanted to see if I could initialize a vector of objects in a

  • 0

I wanted to see if I could initialize a vector of objects in a single line, so I wrote the following example:

vector<Bar*> bar(5, new Bar());

bar[3]->hit++;
for (int i = 0; i < 5; i++)
    cout << bar[i]->hit << endl;

But when I run it, I get:

1
1
1
1
1

It seems to use the same new Bar() for all the pointers. Is it possible to initialize vectors of object pointers to point to different objects?

  • 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-12T23:20:21+00:00Added an answer on June 12, 2026 at 11:20 pm

    All your pointers point to the same location, so the result is expected.

    vector<Bar*> bar(5, new Bar());
    

    doesn’t create a new Bar for all 5 objects, but uses the value returned by new Bar(), which is a pointer to a memory location, to generate the vector.

    To get the result you expect, you can use std::generate:

    Bar* newBar() { return new Bar(); }
    
    //...
    
    vector<Bar*> bar(5);
    std::generate(bar.begin(),bar.end(),newBar);
    

    or simply

    std::generate(bar.begin(),bar.end(), []{return new Bar();});
    

    if C++11 is an option.

    The output for this would be

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

Sidebar

Related Questions

I’m playing around with json objects in javascript and wanted see if someone could
I am new with JQuery and I wanted to see if I could do
I wanted to see if I could initialize a global variable to point to
I've run into a little hurdle and wanted to see if somebody could help
I wanted to see if I could make a custom data set to use
I had some old code i wanted to see if I could do with
I'm learning Python and wanted to see if anyone could help break down and
Im new to powershell - so serious noob. But I wanted to see if
This may not be possible but just wanted to see if anyone could point
I recently wanted to see if I could create an excel spreadsheet using c#

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.