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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:23:24+00:00 2026-05-27T13:23:24+00:00

I was wondering if it was possible to create an array of objects when

  • 0

I was wondering if it was possible to create an array of objects when the object needs things passed into it for the constructor. I want something like this:

MyClass *myVar;
myVar = new MyClass[num];  // I would like to specify the array size after declaration
int i = 0;
for(i = 0;i < num;i++)
   myVar[i] = new MyClass(0,0);  // I would also like to populate the array with new objects

I know that this works:

MyClass *myVar;
myVar = new MyClass[num];

but this only works when the constructor has nothing passed into it. Is what I am trying to do possible? If so, how do I do it?

EDIT: I found out how to do it with using arrays. Here is how I did it:

MyClass **myVar;
myVar = new MyClass *[num];
for(i = 0;i < num;i++)
   myVar[0] = new MyClass(0,0);

I would use vectors and such but my teacher has told us to use basic arrays whenever possible. The above solution I actually got from some code my teacher wrote. Thank you all for your help!

  • 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-27T13:23:25+00:00Added an answer on May 27, 2026 at 1:23 pm
    MyClass *myVar;
    myVar = new MyClass[num];
    

    Actually in this form you cannot invoke constructor which takes parameter(s). It is not allowed by the language specification.

    However, if you use std::vector, which I recommend you to use, then you can create a vector calling non-default constructor as:

    #include <vector> //header file where std::vector is defined
    
    std::vector<MyClass>  arr(num, MyClass(10,20));
    

    It creates a vector of num elements, each element is created by calling copy-constructor of the class, passing MyClass(10,20) as argument to it.

    The vector is also good because now you dont need to manage memory yourself. Neither manual allocation, nor manual deallocation. Plus, you can know the number of elements by calling arr.size() anytime. You always know how many elements the vector contains. You can also add elements anytime, just by calling .push_back() member function as:

    arr.push_back(MyClass(20,30)); 
    

    And now you can access elements, just like you access array, i.e by using index:

    f(arr[i]); // 0 <= i < arr.size();
    

    Additionally, you can use iterators which facilitate idiomatic programming, enabling you to use various algorithmic functions from <algorithm> header as:

    #include <algorithm> //header file where std::for_each is defined
    
    std::for_each(arr.begin(), arr.end(), f);
    

    where f is function which takes one argument of type MyClass& (or MyClass const &) depending on what you want to do in f.

    In C++11, you can use lambda as:

    std::for_each(arr.begin(), arr.end(), [](const MyClass & m)
                                          {
                                               //working with m 
                                          });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if it's possible to create a dynamic linq query using linq to objects.
I was wondering if it is possible to create a flash movie that rotates
Just wondering if it is possible to dynamically create a list of strings in
I was wondering if it were possible to dynamically create an XML layout file
I was wondering if the following is possible. Create a class that accepts an
I was wondering if its possible to inject a thread into a remote app
I'm trying to create a little Ruby hack to make something like the reverse
I was wondering if it was possible to create a cookie (using jquery.cookie) which
I'm wondering is that possible to create new eamil account using Exchange Web Service?
How can I create a String object from a byte array byte arr[MAX_SIZE]; //

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.