constructor initialization list, array of objects

J

j j

Hi

How can I initialize an array of objects in a constructor initialization
list with a specific values. The following does not work.

#include <iostream>

using namespace std;

struct Wheel {
int radius;

Wheel (int radius) : radius (radius) {}
};

struct Car {
Wheel wheels[2];

Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
};

int main (void)
{
Car c1(1, 2);
Car c2;
return 0;
}
 
A

Alain Ketterlin

j j said:
How can I initialize an array of objects in a constructor initialization
list with a specific values. The following does not work.

#include <iostream>
using namespace std;
struct Wheel {
int radius;
Wheel (int radius) : radius (radius) {}
};
struct Car {
Wheel wheels[2];
Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
};

Does compile with gcc-4.6.3, requires -std=c++0x though to suppress
warnings (which, btw, are perfectly informative).

-- Alain.
 
A

Alf P. Steinbach

Hi

How can I initialize an array of objects in a constructor initialization
list with a specific values. The following does not work.

#include<iostream>

using namespace std;

struct Wheel {
int radius;

Wheel (int radius) : radius (radius) {}
};

struct Car {
Wheel wheels[2];

Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
};

int main (void)
{
Car c1(1, 2);
Car c2;
return 0;
}

With a compiler that supports C++11 brace initializers the above
compiles directly.

With Visual C++ you can instead do a bit of hybrid C++03 / C++11:


<code>
#include <iostream>
#include <array>
using namespace std;

struct Wheel {
int radius;
Wheel (int radius) : radius (radius) {}
};

struct Car {
static array<Wheel, 2> values( Wheel const& w1, Wheel const& w2 )
{
array<Wheel, 2> const result = { w1, w2 };
return result;
}

array<Wheel, 2> wheels;

Car () : wheels( values( 5, 6 ) ) {}
Car (int r1, int r2) : wheels( values( r1, r2 ) ) {}
};

int main()
{
Car c1(1, 2);
Car c2;
}
<code>


Cheers & hth.,

- Alf
 
J

j j

W dniu niedziela, 17 czerwca 2012 14:53:26 UTC+2 użytkownik Alf P. Steinbach napisał:
Hi

How can I initialize an array of objects in a constructor initialization
list with a specific values. The following does not work.

#include<iostream>

using namespace std;

struct Wheel {
int radius;

Wheel (int radius) : radius (radius) {}
};

struct Car {
Wheel wheels[2];

Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
};

int main (void)
{
Car c1(1, 2);
Car c2;
return 0;
}

With a compiler that supports C++11 brace initializers the above
compiles directly.

In fact, the brace initializers are supported by C++ 11 but the above example
does not compile using g++ --std=c++0x (g++ version 4.4.5).
 
A

Alf P. Steinbach

W dniu niedziela, 17 czerwca 2012 14:53:26 UTC+2 użytkownik Alf P. Steinbach napisał:
Hi

How can I initialize an array of objects in a constructor initialization
list with a specific values. The following does not work.

#include<iostream>

using namespace std;

struct Wheel {
int radius;

Wheel (int radius) : radius (radius) {}
};

struct Car {
Wheel wheels[2];

Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
};

int main (void)
{
Car c1(1, 2);
Car c2;
return 0;
}

With a compiler that supports C++11 brace initializers the above
compiles directly.

In fact, the brace initializers are supported by C++ 11 but the above example
does not compile using g++ --std=c++0x (g++ version 4.4.5).

First, brace initializers are not "supported" by C++11, they (in the
general form of C++11) are defined by C++11.

Secondly, the code compiles fine with g++ 4.6.1, from which you can
conclude that either you've done something wrong (most likely), or C++11
brace initializers are not supported by your earlier version.

Summing up, that was a pretty dumb comment. Instead of trying to assert
dubious "in fact"'s, just *ask* when you're confused. Also try posting
with your real name. Posting anonymously you are signaling that you're
an adolescent or pre-teen who is not too serious.

Cheers & hth.

- Alf
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top