473,491 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Object construction

I have a simple class:

class Point {
public:
Point() : xval(0), yval(0) {}
Point(int x, int y) : xval(x), yval(0) {}

private:
int xval, yval;
};

The Point is the member of another class:

class UP {
public:
UP() : u(0) {} // line 1
UP(int x, int y) : p(x, y), u(0) {}

private:
int u;
Point p;
};

My question is:

1) How member object is constructed (in this case, Point p)? Does the
compiler initializes member object, then calls constructors, or member
objects are initialized by constructors?

2) If member objects are initialized by constructors, does the code in line
1 should be:

UP() : p(), u(0) {} or UP() : p, u(0)

if line 1 is correct, how p is initialized?
Thank!
Jul 22 '05 #1
2 1191
newbiecpp wrote:
I have a simple class:

class Point {
public:
Point() : xval(0), yval(0) {}
Point(int x, int y) : xval(x), yval(0) {}
You mean

Point(int x, int y) : xval(x), yval(y) {}

private:
int xval, yval;
};

The Point is the member of another class:

class UP {
public:
UP() : u(0) {} // line 1
UP(int x, int y) : p(x, y), u(0) {}
It is generally recommended to keep the order of initialisers in the
list the same as the actual order of initialisation:

UP(int x, int y) : u(0), p(x, y) {}

to avoid potential confusion. It doesn't change anything, really,
just promotes good practice.

private:
int u;
Point p;
};

My question is:

1) How member object is constructed (in this case, Point p)? Does the
compiler initializes member object, then calls constructors, or member
objects are initialized by constructors?
Yes. Memory is allocated, then a constructor is called with arguments
that you provide in the initialiser that corresponds to that member.

2) If member objects are initialized by constructors, does the code in line
1 should be:

UP() : p(), u(0) {} or UP() : p, u(0)

if line 1 is correct, how p is initialized?


If 'p' is missing from the initialiser list, but it's a class object
and it has its own default constructor, then it's default-initialised.
If 'p' didn't have the default constructor, but had the parameterised
one, the program would be ill-formed. If 'p' were a POD, it would be
uninitialised (if missing from the initialiser list).

Victor
Jul 22 '05 #2

"newbiecpp" <ne*******@yahoo.com> wrote in message
news:aHSLc.14$gM6.7@trndny01...
I have a simple class:

class Point {
public:
Point() : xval(0), yval(0) {}
Point(int x, int y) : xval(x), yval(0) {}

private:
int xval, yval;
};

The Point is the member of another class:

class UP {
public:
UP() : u(0) {} // line 1
UP(int x, int y) : p(x, y), u(0) {}

private:
int u;
Point p;
};

My question is:

1) How member object is constructed (in this case, Point p)? Does the
compiler initializes member object, then calls constructors, or member
objects are initialized by constructors?
The member object is initialized by determining what constructor to invoke.
A constructor is not "called", its invoked. Thats what p(x, y) in UP's
alternate constructor's initialization list specifies. You could have
specified the default constructor as:
UP() : p(), u(0) { }
or
UP() : p(0, 0), u(0) { }
thats your freedom...

2) If member objects are initialized by constructors, does the code in line 1 should be:

UP() : p(), u(0) {} or UP() : p, u(0)

if line 1 is correct, how p is initialized?
Your line 1 above would have called the default cstor for p. Its really just
a question of style and documentation, best to specify which constructor to
invoke. Keeps you and the next person to read the code sane.


Thank!

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1547
by: Anthony | last post by:
Hi, Can anyone help me out here, I'm looking for a design (pattern?) for a-synchronic construction of a class. I need this design in a framework which will run in a multithreaded system. I...
4
1657
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a...
7
1458
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
29
2202
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
0
1635
by: Robert Potthast | last post by:
Hello, I want to make my garbage collector more safe. To make it more safe I need to know if an object has been allocated on the stack or on the heap using the operator new. My garbage collector...
6
2311
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
6
2650
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
1
1857
by: ChasW | last post by:
Im looking for some technical clarification regarding exactly what the differences, if any, are between the two pieces of code are, aside from the obvious syntactical differences. int x = 5; ...
4
4141
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
5
3321
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
0
7118
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
7192
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
6862
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
7364
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
5452
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,...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
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.