Constructor and default values

A

Andrea Crotti

I have a situation where the default value for an object is not so
simple, so I ended up doing

PadCoordinate::padCoordinate()
{
for (int i=0; i < GLOBALS::num_landmarks; ++i) {
coord.push_back(NOT_CONNECTED);
}
}

it's just a vector of elements.
Now I'm hunting a bug and I basically understood what the problem is.

In one situation (my tests)
PadCoordinate pc;
pc.coord.size() == 3 (for example)

but in the real world program
PadCoordinate pc;
pc.coord.size() == 0

So actually that thing is not executed "in time".

So first of all what could be the problem?

Second I thought something like this
static vector<coord_t> makeEmpty() {
vector<coord_t> tmp;
for (int i=0; i < GLOBALS::num_landmarks; ++i) {
tmp.push_back(NOT_CONNECTED);
}
return tmp;
}

and then the constructor becomes

PadCoordinate::padCoordinate() : coord(PadCoordinate::makeEmpty())

does it make sense? Or is better to create a static variable which
represents the default value copy and copy it?
 
A

Andrea Crotti

Andrea Crotti said:
I have a situation where the default value for an object is not so
simple, so I ended up doing

PadCoordinate::padCoordinate()
{
for (int i=0; i < GLOBALS::num_landmarks; ++i) {
coord.push_back(NOT_CONNECTED);
}
}

it's just a vector of elements.
Now I'm hunting a bug and I basically understood what the problem is.

In one situation (my tests)
PadCoordinate pc;
pc.coord.size() == 3 (for example)

but in the real world program
PadCoordinate pc;
pc.coord.size() == 0

So actually that thing is not executed "in time".

So first of all what could be the problem?

Second I thought something like this
static vector<coord_t> makeEmpty() {
vector<coord_t> tmp;
for (int i=0; i < GLOBALS::num_landmarks; ++i) {
tmp.push_back(NOT_CONNECTED);
}
return tmp;
}

and then the constructor becomes

PadCoordinate::padCoordinate() : coord(PadCoordinate::makeEmpty())

does it make sense? Or is better to create a static variable which
represents the default value copy and copy it?

Of course right after I posted I found out that the problem had nothing
to do with the constructor, it was just the configuration file not
reachable from the "real world".
But the question remains valid, what's the best way to have more complex
default values for a class?
 
J

Jeff Flinn

Andrea said:
Of course right after I posted I found out that the problem had nothing
to do with the constructor, it was just the configuration file not
reachable from the "real world".
But the question remains valid, what's the best way to have more complex
default values for a class?

You don't provide many of the types or declarations in your question, so
I'm making some assumptions here. Such as PadCoordinate::coord is a
vector<coord_t>, and NOT_CONNECTED evaulates to an instance of type
coord_t. Then you can just do:

PadCoordinate::padCoordinate()
: coord(GLOBALS::num_landmarks, NOT_CONNECTED)
{}

which just uses the the std::vector<Elem>(count, Elem) constructor. The
std::vector<Elem>(count) constructor could be used if coord_t() ==
NOT_CONNECTED.

Jeff
 
A

Andrea Crotti

Jeff Flinn said:
You don't provide many of the types or declarations in your question,
so I'm making some assumptions here. Such as PadCoordinate::coord is
a vector<coord_t>, and NOT_CONNECTED evaulates to an instance of type
coord_t. Then you can just do:

PadCoordinate::padCoordinate()
: coord(GLOBALS::num_landmarks, NOT_CONNECTED)
{}

which just uses the the std::vector<Elem>(count, Elem)
constructor. The std::vector<Elem>(count) constructor could be used if
coord_t() == NOT_CONNECTED.

Jeff

Ah great thanks, it's funny because I used that way of constructing
vectors many times, but in the default constructor I just didn't see it
possible (of course with no reason).

If it wasn't that simple and I actually needed a loop what's the best way?
 
J

James Kanze

Ah great thanks, it's funny because I used that way of constructing
vectors many times, but in the default constructor I just didn't see it
possible (of course with no reason).
If it wasn't that simple and I actually needed a loop what's the best way?

I often use the two iterator constructors, with a static
C style array containing the initial values.

In other cases, it may be reasonable to derive from std::vector,
just to provide a custom constructor which does the desired
initialization. (In the case of a class member, this is really
only interesting if the member is const. Otherwise, there's
nothing wrong with the way you were doing it.)
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top