Connecting Tech Pros Worldwide Help | Site Map

Object construction

  #1  
Old July 22nd, 2005, 05:46 PM
newbiecpp
Guest
 
Posts: n/a
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!


  #2  
Old July 22nd, 2005, 05:46 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Object construction


newbiecpp wrote:
[color=blue]
> I have a simple class:
>
> class Point {
> public:
> Point() : xval(0), yval(0) {}
> Point(int x, int y) : xval(x), yval(0) {}[/color]

You mean

Point(int x, int y) : xval(x), yval(y) {}
[color=blue]
>
> 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) {}[/color]

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.
[color=blue]
>
> 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?[/color]

Yes. Memory is allocated, then a constructor is called with arguments
that you provide in the initialiser that corresponds to that member.
[color=blue]
>
> 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?[/color]

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
  #3  
Old July 22nd, 2005, 05:46 PM
SaltPeter
Guest
 
Posts: n/a

re: Object construction



"newbiecpp" <newbiecpp@yahoo.com> wrote in message
news:aHSLc.14$gM6.7@trndny01...[color=blue]
> 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?[/color]

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...
[color=blue]
>
> 2) If member objects are initialized by constructors, does the code in[/color]
line[color=blue]
> 1 should be:
>
> UP() : p(), u(0) {} or UP() : p, u(0)
>
> if line 1 is correct, how p is initialized?[/color]

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.
[color=blue]
>
>
> Thank!
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object construction... barcaroller answers 2 June 27th, 2008 05:43 PM
Peformance of Deserialization vs new Object construction Brian Richards answers 1 January 10th, 2006 09:45 AM
Peformance of Deserialization vs new Object construction Brian Richards answers 1 January 10th, 2006 09:45 AM
Data Saving/Loading & Object Construction Jerivix Entadi answers 4 July 22nd, 2005 07:47 AM
ASP & COM+'s enable object construction Dan Avni answers 4 July 19th, 2005 11:56 AM