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; } 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(); } std::array. Anyway, a raw array works too.i can be casted to myClass. I don't know who up-voted your answer.myClass(int i=0) (Omitting explicit, which is a really bad idea).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)
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(); } 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]; } std::vector instead. (and you miss one pointer BTW).
myClass myInstance[i];out of the loop body, declaring it above the for-loop asmyClass 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).-Wall -Wextra -pedanticand an option to select the newest C++ standard wouldn't be amiss either. The compiler should then complain loudly over your code.