By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,132 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,132 IT Pros & Developers. It's quick & easy.

Member object in initializer list

P: n/a
How do you initialize objects in the initializer list?

Also, while this code is probably incorrect, it does compile (and execute)
and I wonder what the actual result is.

class Control {
Point origin;
public:
Control(int x, int y);
}

Control::Control(int x, int y) : origin(x, y) {
// What happened in the initializer list?
}
Dec 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Ham Pastrami" <no****@dot.comwrote in message
news:SU*******************@newssvr21.news.prodigy. net
How do you initialize objects in the initializer list?

Also, while this code is probably incorrect, it does compile (and
execute) and I wonder what the actual result is.

class Control {
Point origin;
public:
Control(int x, int y);
}
Missing semi-colon.
>
Control::Control(int x, int y) : origin(x, y) {
// What happened in the initializer list?
}
Assuming Point has a constructor that takes two int arguments, the code is
correct and the Point member object is constructed in the same way as a
non-member object would be constructed if written as:

Point origin(x,y);
--
John Carson

Dec 29 '06 #2

P: n/a

Ham Pastrami wrote:
How do you initialize objects in the initializer list?

Also, while this code is probably incorrect, it does compile (and execute)
and I wonder what the actual result is.

class Control {
Point origin;
public:
Control(int x, int y);
}
;
>
Control::Control(int x, int y) : origin(x, y) {
// What happened in the initializer list?
}
Assuming Point's ctor is available and matches the signature
Point(int,int), the init list invoked Point's ctor. The Control class
could have also provided a default ctor:

class Control {
Point origin;
public:
Control();
Control(int x, int y);
};

Control::Control() : origin(0, 0) { } // or origin( ) if Point itself
has a default ctor
Control::Control(int x, int y) : origin(x, y) { } // invokes
Point(int,int)

And there is yet another way to do all of the above:

class Control {
Point origin;
public:
Control(int x = 0, int y = 0);
};

Dec 29 '06 #3

P: n/a

Salt_Peter wrote:
class Control {
Point origin;
public:
Control();
Control(int x, int y);
};

Control::Control() : origin(0, 0) { } // or origin( ) if Point itself
has a default ctor
Control::Control(int x, int y) : origin(x, y) { } // invokes
Point(int,int)

And there is yet another way to do all of the above:

class Control {
Point origin;
public:
Control(int x = 0, int y = 0);
};
That's not equivalent to your first example. As well as allowing these
two forms of construction

Control c1;
Control c2(42, 42);

the default parameters allow this

Control c3(1); // x is 1, y is zero.

which may well not be what is wanted. Moreover, because the single
argument constructor is not explicit, this is also allowed

Control c4(1, 1);
c4 = 42;
c4 = '\n';

which is very likely not what is wanted.

If it only makes sense to construct a Control with zero arguments or
with two arguments, give it two constructors (as in your first
example). If it does make sense to construct with only one argument
then the default parameters are OK. For me, the opportunity to save
some typing at the point of construction is not enough of a reason. I
would only use the default parameters if y == 0 really was a sensible
default behaviour and it was an unusual special case for y to need a
different value. If you choose that route, declare the constructor
explicit in the class unless it really does make sense to be able to
implicitly convert from, for example, a char to a Control.

class Control {
public:
explicit Control(int x = 0, int y = 0) {}
};

int main()
{
Control c4(1, 1);
c4 = 42; // Not allowed with explicit constructor
c4 = '\n'; // Not allowed with explicit constructor
}

Gavin Deane

Dec 29 '06 #4

P: n/a

Gavin Deane wrote:
Salt_Peter wrote:
class Control {
Point origin;
public:
Control();
Control(int x, int y);
};

Control::Control() : origin(0, 0) { } // or origin( ) if Point itself
has a default ctor
Control::Control(int x, int y) : origin(x, y) { } // invokes
Point(int,int)

And there is yet another way to do all of the above:

class Control {
Point origin;
public:
Control(int x = 0, int y = 0);
};

That's not equivalent to your first example. As well as allowing these
two forms of construction

Control c1;
Control c2(42, 42);

the default parameters allow this

Control c3(1); // x is 1, y is zero.

which may well not be what is wanted. Moreover, because the single
argument constructor is not explicit, this is also allowed

Control c4(1, 1);
c4 = 42;
c4 = '\n';

which is very likely not what is wanted.

If it only makes sense to construct a Control with zero arguments or
with two arguments, give it two constructors (as in your first
example). If it does make sense to construct with only one argument
then the default parameters are OK. For me, the opportunity to save
some typing at the point of construction is not enough of a reason. I
would only use the default parameters if y == 0 really was a sensible
default behaviour and it was an unusual special case for y to need a
different value. If you choose that route, declare the constructor
explicit in the class unless it really does make sense to be able to
implicitly convert from, for example, a char to a Control.

class Control {
public:
explicit Control(int x = 0, int y = 0) {}
};

int main()
{
Control c4(1, 1);
c4 = 42; // Not allowed with explicit constructor
c4 = '\n'; // Not allowed with explicit constructor
}

Gavin Deane
Good point, thanks

Dec 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.