How can i call non default constructor for array of object
e,g
myClass array[8]???//what to wrte here :mad:
Printable View
How can i call non default constructor for array of object
e,g
myClass array[8]???//what to wrte here :mad:
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();
}
Code:class Test {
public:
test();
private:
int t[5];
};
Test::test(){
t[ 2 ] = 2;
}
And that has any relevance to the question, how?Quote:
Originally Posted by cyberCLoWn
It doesn't. Oh wait... damn... those rhetorical questions always get me.