How can i call non default constructor for array of object
e,g
myClass array[8]???//what to wrte here
How can i call non default constructor for array of object
e,g
myClass array[8]???//what to wrte here
you can try something like this:
Code:class myClass { int num; public: myClass(int num_) : num(num_) {}; }; int num_ = 0; myClass * array = new myClass[8](num_);
You can try, but.....
>> error C2538: new : cannot specify initializer for arrays
You'll have allocate individual elements of the array dynamically if you want to use a non-default constructor.
gg
>You'll have allocate individual elements of the array dynamically if you want to use a non-default constructor.
Or initialize an array manually:
Code:#include <iostream> using namespace std; class test { int i; public: test ( int init ) : i ( init ) {} void print() { cout<< i <<endl; } }; int main() { test a[5] = { test ( 0 ), test ( 1 ), test ( 2 ), test ( 3 ), test ( 4 ) }; for ( int i = 0; i < 5; i++ ) a[i].print(); }
My best code is written with the delete key.
Code:class Test { public: test(); private: int t[5]; }; Test::test(){ t[ 2 ] = 2; }
And that has any relevance to the question, how?Originally Posted by cyberCLoWn
My best code is written with the delete key.
It doesn't. Oh wait... damn... those rhetorical questions always get me.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie