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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:38:48+00:00 2026-05-21T11:38:48+00:00

Sorry if the title is not clear but I ll explain now my problem.

  • 0

Sorry if the title is not clear but I ll explain now my problem. I am new in C++.

I have created a class in C++. Instances of that class are the input of the program and they have to be stored in an array to perform the calculations. The problem is that the number of instances of that class that have to be defined by the user is fixed for a single run but can vary from run to run. Here an example:

#include <<blablah>blahblah>

int main()
{
int number_of_instances = 3;

MyClass first_instance(one_parameter_1, another_parameter_1);
MyClass second_instance(one_parameter_2, another_parameter_2);
MyClass third_instance(one_parameter_3, another_parameter_3);

///////////////////

NOW I HAVE TO STORE ALL THREE IN AN ARRAY LIKE

MyClass array[number_of_instances] = {first_instance, second_instance, third_instance};

THE PROBLEM IS THAT I DO NOT KNOW BEFORE HAND HOW MANY OF THEM ARE THE USER IS GOING TO INPUT

///////////////////

performCalculations(array);
return 0;
}

Thanks a lot in advance.

  • 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-21T11:38:49+00:00Added an answer on May 21, 2026 at 11:38 am

    The typical C++ solution is to use a vector.

    vector<MyClass> v;
    v.push_back(first_instance);  //add an already instantiated object to the end of the vector
    v.push_back(second_instance);
    v.push_back(third_instance);
    

    You won’t have to worry about memory management and you are able to access the vector like you would a normal array:

    v[0].classMember
    

    You can also add items to the vector in a loop if needed like so:

    for(int i = 0; i < 5; i++){
        v.push_back( MyClass(i, param2) );
    }
    

    And all the objects will be destructed when the vector goes out of scope if you’re storing the objects directly in the vector.

    One of the downsides to storing the objects directly in the vector is passing the vector as a parameter to a function. This will be a slow operation since the vector (and all the objects it holds) will have to be copied.

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

Sidebar

Related Questions

sorry if the title is not very clear. I will try to explain now:
Sorry if title is not too clear but I think it's about right. NEhow,
I'm sorry I could not think of a better title. The problem is the
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry if the post title wasn't clear, I will try to explain a little
Sorry for the not so great title, but I'm curious how people build a
Sorry if the title is not very clear. I was not sure about the
sorry for the title but I'm not sure how I should call it. I'm
Sorry about the title, I'm not exactly sure how to explain this with a

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.