Vector of class with contractor
-
Hello, I want to start a vector of calsses with the same contruct Lets say this is my class :
class aaa { public: aaa(int i){ m_iA = i; } private: int m_iA; };
and I want now to do 5 classes with the same contruct:aaa* f = aaa(2)[5];
I have to do a loop for this (pointer of pointers etc.) ? Or it is possible to do this somehow in a short way ? Thanks, -
Hello, I want to start a vector of calsses with the same contruct Lets say this is my class :
class aaa { public: aaa(int i){ m_iA = i; } private: int m_iA; };
and I want now to do 5 classes with the same contruct:aaa* f = aaa(2)[5];
I have to do a loop for this (pointer of pointers etc.) ? Or it is possible to do this somehow in a short way ? Thanks,Ohad Cabiri wrote:
I have to do a loop for this (pointer of pointers etc.) ?
In fact if you don't have a default constructor, the only way to do that is to have an array of pointers to your class:
aaa** pArray;
...
...
pArray = new aaa*[5];And then loop through your array and create all the classes:
for (int i=0;i<5;i++)
pArray[i] = new aaa(Value);Of course, you will need to delete all these instances AND the array.
Cédric Moonen Software developer
Charting control [v1.1] -
Ohad Cabiri wrote:
I have to do a loop for this (pointer of pointers etc.) ?
In fact if you don't have a default constructor, the only way to do that is to have an array of pointers to your class:
aaa** pArray;
...
...
pArray = new aaa*[5];And then loop through your array and create all the classes:
for (int i=0;i<5;i++)
pArray[i] = new aaa(Value);Of course, you will need to delete all these instances AND the array.
Cédric Moonen Software developer
Charting control [v1.1]So there is no shortcut here. Thanks
-
Hello, I want to start a vector of calsses with the same contruct Lets say this is my class :
class aaa { public: aaa(int i){ m_iA = i; } private: int m_iA; };
and I want now to do 5 classes with the same contruct:aaa* f = aaa(2)[5];
I have to do a loop for this (pointer of pointers etc.) ? Or it is possible to do this somehow in a short way ? Thanks,If a statically-allocated array is acceptable, you can do:
aaa stuff[] = { aaa(2), aaa(2), aaa(2), aaa(2), aaa(2) };
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?