5

How do you create multiple class instances without individually typing in their names?

int main(){ myClass myInstance_1; myClass myInstance_2; myClass myInstance_3; ... myClass myInstace_10; } 
4
  • 2
    Take myClass myInstance[i]; out of the loop body, declaring it above the for-loop as myClass myInstance[10]; If you're looking for a way to pass distinct values to the construction of an array for each member, you're not going to easily do it like this (and in fact may be the real problem you're trying to solve). Commented Oct 11, 2014 at 0:51
  • Do you compile with full warnings? Use -Wall -Wextra -pedantic and an option to select the newest C++ standard wouldn't be amiss either. The compiler should then complain loudly over your code. Commented Oct 11, 2014 at 0:55
  • 1
    IMO and with all the respect: You really need to read a good C++ book. Commented Oct 11, 2014 at 0:57
  • A more complete answer is provided here. Commented Feb 8, 2018 at 19:08

5 Answers 5

7

You may do the following:

int main(){ std::vector<myClass> myInstances; for(int i = 0; i < 10; ++i) { myInstances.emplace_back(i); } myInstances[5].myClassFunction(); } 
Sign up to request clarification or add additional context in comments.

6 Comments

If you insist on a std-container, best use the proper fit: std::array. Anyway, a raw array works too.
+ I think it highly likely this is what is really the OP's goal: a different value for each construction of the element sequence.
Your code will not compile unless integer i can be casted to myClass. I don't know who up-voted your answer.
@icando: See the question, the default ctor is myClass(int i=0) (Omitting explicit, which is a really bad idea).
@icando: See it live. The single parameter constructor provides the very "unless" case you seem to think is missing. And "who up-voted" is in the very comment preceding yours.
|
3

How do you create multiple class instances without individually typing in their names?

The answer is you make an array, but not the way you tried it.

Like this:

int main() { MyClass myInstance[10]; for(int i = 0; i < 10; i++) { myInstance[i].myClassFunction(); } } 

TIP: Don't use arrays, use std::vector or std::array.

(see answer from @Jarod42)

Comments

3

Solution 1: Use an array of integers as an initializer, the constructor is used to convert the integer to myClass.

class myClass { public: //constructor myClass( int num) {m_number = num;}; void myClassFunction(){cout<< "I am " << m_number << endl;} private: int m_number; }; int main(){ myClass myInstance[10] = {0,1,2,3,4,5,6,7,8,9}; for ( int i=0; i< 10; i++) myInstance[i].myClassFunction(); } 

Solution 2: Use a static counter to set m_number, so no need to use a non-default constructor.

class myClass { public: //constructor myClass(){m_number=counter++;}; void myClassFunction(){cout<< "I am " << m_number << endl;} private: int m_number; static int counter; }; int myClass::counter = 0; int main(){ myClass myInstance[10]; for ( int i=0; i< 10; i++) myInstance[i].myClassFunction(); } 

Solution 3: Set the m_number after the constructor call, so the default constructor is enough.

class myClass { public: //constructor void setNum(int num){m_number=num;}; void myClassFunction(){cout<< "I am " << m_number << endl;} private: int m_number; }; int main(){ myClass myInstance[10]; for ( int i=0; i< 10; i++) myInstance[i].setNum(i); for ( int i=0; i< 10; i++) myInstance[i].myClassFunction(); } 

Comments

0

Just like Jarod42's code:

int main(){ std::vector<myClass> myInstances; for(int i = 0; i < 10; ++i) { myInstances.push_back(myClass(i)); } myInstances[5].myClassFunction(); } 

I think these should work.

Comments

-2

Use array:

myClass myInstance[10]; 

This will create an array with 10 instances but each instance will have its m_number set to 0.

Another approach:

myClass *myInstance = new myClass[10]; for (unsigned int i = 0; i < 10; i++) { myInstance[i] = new myClass(i); } 

Here each instance will have different m_number values


Edit for the sake of fixing the problem with above code. Better solutions have already been proposed.

myClass *myInstance[10]; // Allocate for (unsigned int i = 0; i < 10; i++) { myInstance[i] = new myClass(i); } // At the end free for (unsigned int i = 0; i < 10; i++) { delete myInstance[i]; } 

3 Comments

Avoid raw owning pointer, use std::vector instead. (and you miss one pointer BTW).
@Jarod42: Instead of raw owning pointers, the proper alternative is smart-pointers, right? Though any dynamic allocation is no good in this case.
You are allocating 2 times but not deleting?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.