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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:00:25+00:00 2026-05-16T14:00:25+00:00

I want to make a dynamic array of foo, with the number of items

  • 0

I want to make a dynamic array of foo, with the number of items being x. Arguments y and z are to be passed to the constructor of the item foo. I was hoping to do something similar to:

Foo* bar = new Foo(y, z)[x];

However that produced the following compiler error:

 error: expected `;' before '[' token

So after speaking with an experienced friend, he gave me this, which he admitted was a lazy way of doing it, but it works. I was wondering, is there a better/proper way?

Foo* bar = (Foo*) new int[x];
for (int i = 0; i < x; i++) {
    bar[i] = Foo(y, z);
}
  • 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-16T14:00:25+00:00Added an answer on May 16, 2026 at 2:00 pm

    “I want to make a dynamic array” So use a std::vector, it exists for a reason.

    std::vector<foo> bar(x, foo(y, z));
    

    This creates a dynamic array with x elements initialized to foo(y, z).


    The above makes copies of the second parameter, x times. If you want to generate values for the vector, use generate_n:

    std::vector<double> weights;
    std::generate_n(std::back_inserter(weights), x, ...);
    

    You replace ... with a function or functor to call, that returns a value. Generally you make a functor:

    struct generate_weight
    {
        double operator()() const
        {
            return random(1e-3);
        }
    };
    

    Giving:

    std::generate_n(std::back_inserter(weights), x, generate_weight());
    

    If your compiler supports C++0x, you can take advantage of lambda’s. These do the same thing, except they keep the code concise and localized:

    std::generate_n(std::back_inserter(weights), x,
                    [](){ return random(1e-3); } );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make my vertex array dynamic. So that values would be added
I want to make Dynamic UI for a desktop application. I am planning to
i want to make a dynamic list with buttons on it. My button in
If I want to make a rule dynamic so i can use assert after
I want to make a website where the (dynamic in height) header and footer
So, I'm trying to make a dynamic UI, and i want to add a
Is it possible to make a generic class that acts as a dynamic array
I am using jquery ui's tabs And i want to make it dynamic by
I am trying to make a generic method: class Foo attr_reader def add(object) item
I'm working with a dynamic array in Excel VBA. The number of columns (m)

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.