|
|
 | | From: | caypea at yahoo.com | | Subject: | Newbie question: arrays of objects | | Date: | 8 Nov 2004 23:13:11 -0800 |
|
|
 | I am having problems declaring an array of objects of a class that does not have a default constructor. Could someone post the correct syntax please?
Also for classes that have multiple constructors, is there a way to declare an array of objects invoking a specific constructor?
Ciao, Karan
|
|
 | | From: | Andre Dajd | | Subject: | Re: Newbie question: arrays of objects | | Date: | 9 Nov 2004 02:38:14 -0800 |
|
|
 | caypea@yahoo.com wrote in message news:<9a9c88a4.0411082313.12273740@posting.google.com>... > I am having problems declaring an array of objects of a class that > does not have a default constructor. Could someone post the correct > syntax please? > > Also for classes that have multiple constructors, is there a way to > declare an array of objects invoking a specific constructor? > > Ciao, > Karan
You actually, only need copy constructibility, which is provided automatically, unless you prohibit it. That is, you can create an object of a class somehow and then, provided that a copy-constructor is implemented or autogenerated, use the object as the initializer
struct s { int i; s(int j): i(j) {} private: s(); };
int main() { s i(10); s* x= new s[10](i); // using automatically generated copy constructor
delete[] x; return 1; }
This will not work if you deliberately prohibit copy constructor in the same way i did prohibit the default one.
|
|
 | | From: | John Harrison | | Subject: | Re: Newbie question: arrays of objects | | Date: | Tue, 9 Nov 2004 10:44:59 -0000 |
|
|
 | "Andre Dajd" wrote in message news:4db41123.0411090238.31b7da4c@posting.google.com... > caypea@yahoo.com wrote in message news:<9a9c88a4.0411082313.12273740@posting.google.com>... > > I am having problems declaring an array of objects of a class that > > does not have a default constructor. Could someone post the correct > > syntax please? > > > > Also for classes that have multiple constructors, is there a way to > > declare an array of objects invoking a specific constructor? > > > > Ciao, > > Karan > > You actually, only need copy constructibility, which is provided > automatically, unless you prohibit it. That is, you can create an > object of a class somehow and then, provided that a copy-constructor > is implemented or autogenerated, use the object as the initializer > > struct s > { > int i; > > s(int j): i(j) {} > > private: > s(); > }; > > int main() > { > s i(10); > > s* x= new s[10](i); // using automatically generated copy > constructor >
That is not legal C++. You might have a compiler that accepts it, but that doesn't make it legal.
john
|
|
 | | From: | Andre Dajd | | Subject: | Re: Newbie question: arrays of objects | | Date: | 9 Nov 2004 10:07:29 -0800 |
|
|
 | "John Harrison" wrote in message news:<2vbl98F2jabb2U1@uni-berlin.de>... > "Andre Dajd" wrote in message > news:4db41123.0411090238.31b7da4c@posting.google.com... > > caypea@yahoo.com wrote in message > news:<9a9c88a4.0411082313.12273740@posting.google.com>... [snip] > > struct s > > { > > int i; > > > > s(int j): i(j) {} > > > > private: > > s(); > > }; > > > > int main() > > { > > s i(10); > > > > s* x= new s[10](i); // using automatically generated copy > > constructor > > > > That is not legal C++. You might have a compiler that accepts it, but that > doesn't make it legal. > > john
Interesting...
Standard 5.3.4 (first version, though) apparently provides for this form, i.e. you may have optional "new-initializer", which should not necessarily be empty, as explained in subsection 15, point 3... Maybe I misinterpret it, but this meaning appears quite natural. The standard is, indeed, a bit vague there, as it use wording "initializes that object", not, ruling out a possibility of combining the array declarator with initializer.
The compiler used was GCC 3.3 under the latest Dev-CPP. Out of curiosity, I have added the copy constructor to the above example and put a static counter into the class, to see if the copy constructor would be called. It was.
At the same time the code did not compile with VS6 and Comeau (through their web interface).
Anyway, I have to withdraw my comment in it's generality and confine it only to the lucky GCC users :)
Rgds d
|
|
 | | From: | Sharad Kala | | Subject: | Re: Newbie question: arrays of objects | | Date: | Tue, 9 Nov 2004 13:32:54 +0530 |
|
|
 | wrote in message > I am having problems declaring an array of objects of a class that > does not have a default constructor. Could someone post the correct > syntax please?
> Also for classes that have multiple constructors, is there a way to > declare an array of objects invoking a specific constructor?
Yes, even if it's not very convenient.
struct MyClass{ MyClass(int i){ } };
int main() { MyClass arr[3] = {MyClass(2), MyClass(3), MyClass(4)}; }
Sharad
|
|
 | | From: | John Harrison | | Subject: | Re: Newbie question: arrays of objects | | Date: | Tue, 9 Nov 2004 07:23:28 -0000 |
|
|
 | wrote in message news:9a9c88a4.0411082313.12273740@posting.google.com... >I am having problems declaring an array of objects of a class that > does not have a default constructor. Could someone post the correct > syntax please?
There is no syntax.
> > Also for classes that have multiple constructors, is there a way to > declare an array of objects invoking a specific constructor? >
It is impossible to declare an array with anything that does not have a default constructor, or to invoke any constructor other than the default constructor.
One answer is to use a vector instead. Do you know about vectors?
john
|
|
 | | From: | Sharad Kala | | Subject: | Re: Newbie question: arrays of objects | | Date: | Tue, 9 Nov 2004 13:30:32 +0530 |
|
|
 | "John Harrison" wrote in message > > wrote in message > news:9a9c88a4.0411082313.12273740@posting.google.com... > >I am having problems declaring an array of objects of a class that > > does not have a default constructor. Could someone post the correct > > syntax please? > > There is no syntax. > > > > > Also for classes that have multiple constructors, is there a way to > > declare an array of objects invoking a specific constructor? > > > > It is impossible to declare an array with anything that does not have a > default constructor, or to invoke any constructor other than the default > constructor.
Not quite. It is possible even if it isn't very convenient.
MyClass arr[3] = {MyClass(2), MyClass(3), MyClass(4)};
Sharad
|
|
 | | From: | John Harrison | | Subject: | Re: Newbie question: arrays of objects | | Date: | Tue, 9 Nov 2004 08:30:27 -0000 |
|
|
 | "Sharad Kala" wrote in message news:2vbbo4F2ihj6dU1@uni-berlin.de... > > "John Harrison" wrote in message > >> wrote in message >> news:9a9c88a4.0411082313.12273740@posting.google.com... >> >I am having problems declaring an array of objects of a class that >> > does not have a default constructor. Could someone post the correct >> > syntax please? >> >> There is no syntax. >> >> > >> > Also for classes that have multiple constructors, is there a way to >> > declare an array of objects invoking a specific constructor? >> > >> >> It is impossible to declare an array with anything that does not have a >> default constructor, or to invoke any constructor other than the default >> constructor. > > Not quite. It is possible even if it isn't very convenient.
Sorry, me being completely stupid. No cup of coffee yet. I was of course thinking of dynamic memory allocation not a statically or locally declared array.
> > MyClass arr[3] = {MyClass(2), MyClass(3), MyClass(4)};
Assuming MyClass has a non-explicit constructor then simply
MyClass arr[3] = {2, 3, 4};
OP, perhaps you could post the code with which you are having trouble. It's the best way to get high quality answers (and helps avoid the odd completely incorrect answer like my previous one).
john
|
|
|