Object construction

Discussion in 'C++' started by newbiecpp, Jul 22, 2004.

  1. newbiecpp

    newbiecpp Guest

    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!
     
    newbiecpp, Jul 22, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jul 22, 2004
    #2
    1. Advertising

  3. newbiecpp

    SaltPeter Guest

    "newbiecpp" <> 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!
    >
    >
     
    SaltPeter, Jul 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anthony
    Replies:
    1
    Views:
    314
    Victor Bazarov
    Sep 23, 2003
  2. Jerivix Entadi
    Replies:
    4
    Views:
    388
    John Harrison
    Feb 23, 2004
  3. Dave
    Replies:
    7
    Views:
    392
    Default User
    Jan 11, 2005
  4. Ook
    Replies:
    10
    Views:
    572
  5. cantide5ga
    Replies:
    7
    Views:
    398
    cantide5ga
    Oct 23, 2007
Loading...

Share This Page