Newbie question: arrays of objects

C

caypea

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
 
J

John Harrison

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
 
S

Sharad Kala

John Harrison said:
There is no syntax.


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
 
S

Sharad Kala

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
 
J

John Harrison

Sharad Kala said:
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
 
A

Andre Dajd

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.
 
J

John Harrison

Andre Dajd said:
(e-mail address removed) 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?

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
 
A

Andre Dajd

John Harrison said:
Andre Dajd said:
(e-mail address removed) wrote in message news:<[email protected]>... [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
 
O

Old Wolf

John Harrison said:
Assuming MyClass has a non-explicit constructor then simply

MyClass arr[3] = {2, 3, 4};

Does that always work?

Here is my code: (Sorry if this is a report -- I posted a few
days ago under another thread but it didn't seem to show up).

#include <string>

typedef std::string M[3][2];
//typedef char M[3][2][33];

struct X
{
M a;
};

int main()
{
M p = { {"a","b"}, {"c","d"}, {"e","f"} };
M q = { {"a","b"}, {"c","d"}, {"e"} };
M r = { {"a","b"}, {"c","d"} };

X x = { { {"a","b"}, {"c","d"}, {"e","f"} } };
X y = { { {"a","b"}, {"c","d"}, {"e"} } };
X z = { { {"a","b"}, {"c","d"} } };
}

g++ 3.4.1 compiles p,q,r,x,y OK but gives an ICE on z.
bcc 5.5.1 compiles p,q, ICEs on r, and says 'Syntax error' on x,y,z.
If I change the typedef back to char from string, it all works.
 
J

John Harrison

Old Wolf said:
John Harrison said:
Assuming MyClass has a non-explicit constructor then simply

MyClass arr[3] = {2, 3, 4};

Does that always work?

Here is my code: (Sorry if this is a report -- I posted a few
days ago under another thread but it didn't seem to show up).

#include <string>

typedef std::string M[3][2];
//typedef char M[3][2][33];

struct X
{
M a;
};

int main()
{
M p = { {"a","b"}, {"c","d"}, {"e","f"} };
M q = { {"a","b"}, {"c","d"}, {"e"} };
M r = { {"a","b"}, {"c","d"} };

X x = { { {"a","b"}, {"c","d"}, {"e","f"} } };
X y = { { {"a","b"}, {"c","d"}, {"e"} } };
X z = { { {"a","b"}, {"c","d"} } };
}

g++ 3.4.1 compiles p,q,r,x,y OK but gives an ICE on z.
bcc 5.5.1 compiles p,q, ICEs on r, and says 'Syntax error' on x,y,z.
If I change the typedef back to char from string, it all works.

Should do, although because some of your initialisers are 'short' you need a
default constructor for the missing items (std::string has this of course).
VC++ 7.1 and Comeau compile all you examples without problems.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top