PDA

View Full Version : syntax for C++ initializer list?


WhatMeWorry
2006.06.20, 09:11 PM
typedef struct Point3D
{
double x; // latitude position
double y; // longitude position
double z; // altitude position
} Point3D;


class Body
{
public:
Body(); // default consructor takes no arguments
~Body();

Point3D position;
double heading;


Body::Body()
: position.x(0.0),
position.y(0.0),
position.z(0.0),
heading(0.0)
{

Keeps bombing out at the : position.x(0.0)
I've tried position->x(0.0), x(0.0) and other clueless stabs in
the dark.

I assume this is trivial but please, humilate me in front of the world :)

Blacktiger
2006.06.20, 09:30 PM
I think that C++ has not yet created space for the Point3D structure. The solution to this is to not use the initializer list here. Changing to position->(0.0) would do nothing since position is not a pointer.

This should work though...

Body::Body(): heading(0.0)
{
position.x(0.0);
position.y(0.0);
position.z(0.0);
}

OneSadCookie
2006.06.20, 09:51 PM
You would need to give Point3D a constructor in order to be able to initialize it in the initializer list I think. The only thing that might possibly work is

Body::Body() : position({0.0, 0.0, 0.0}) {}

NCarter
2006.06.21, 10:18 AM
I'm almost certain (without trying it) that you can't use BlackTiger's 'position.x(0.0);' notation. You'd need to assign the value using 'position.x = 0.0;' instead.

I'd agree with OSC's suggestion that Point3D should have a constructor like this:

Point3D(double inX, double inY, double inZ) : x(inX), y(inY), z(inZ) {}

That would allow Body's constructor to be:

Body() : position(0.0, 0.0, 0.0), heading(0.0) {}

You might also want to have a default constructor like this:

Point3D() : x(0.0), y(0.0), z(0.0) {}

Then you wouldn't need to mention points which should be zero in your initialiser list at all.

One last thing - there's no need to use C-style 'typedef struct Name {...} Name;' in C++. You should write this instead:

struct Point3D
{
// Constructors here if you like

double x; // latitude position
double y; // longitude position
double z; // altitude position
};

jfaller
2006.06.21, 11:43 AM
Point3D() : x(0.0), y(0.0), z(0.0) {}


Best would just be to have 1 Point constructor with default arguments:


Point3D(float inX = 0.0, float inY = 0.0, float inZ = 0.0)
: x(inX), y(inY), z(inZ)
{ }

WhatMeWorry
2006.06.21, 01:49 PM
[QUOTE]struct Point3D
{
// Constructors here if you like

double x; // latitude position
double y; // longitude position
double z; // altitude position
};[/QUOTE


Thanks all. I now see that position is not a "primitive" type and so needs a constructor.
However, does this mean that Point3D needs to be a class? Or does the above
pattern imply that a constructor can be within just a C++ struct? I hope so, because
that would be pretty sweet.

NCarter
2006.06.21, 02:25 PM
However, does this mean that Point3D needs to be a class? Or does the above pattern imply that a constructor can be within just a C++ struct?
It can be a class or a struct, whichever you prefer. The two things are equivalent in every way in C++, except that struct members are public by default and class members are private by default. You can therefore have constructors, methods and anything else you like in a C++ struct.

I'd probably make Point3D a class, but it's entirely up to you. :)