473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Member object in initializer list

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
4 1448
"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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
1
by: Derek | last post by:
I get the following warning warning C4355: 'this' : used in base member initializer list when I compile the following program with VC6 and VC7 (the latest GCC and Comeau doesn't think anything...
3
by: santosh | last post by:
Hello, I have const member in the class. How can I initialise these. I can not initialise in constructor ,(it is giving compilation error) What is the proper way to initialise. The code is given...
3
by: James | last post by:
I have a base class that has constructor Person(string name, int age) and a derived class Empolyee(string job_title, int salary) When I try to call it using new Employee(name, age, job_title,...
5
by: meyousikmann | last post by:
Given these two (incomplete but representative) classes in two seperate header files: Class1.h class Class1 { public: Class(const char CharValue, const int IntValue1, const int IntValue2);...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
3
by: Sambo | last post by:
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near...
11
by: dalu.gelu | last post by:
Hi, can anyone help me by writing a sample code of defining a copy constructor in a class having data member as an object of another class. for eg: class A{ int x; public: A(){ x=6;} };
3
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.