Reference to class question

Discussion in 'C++' started by Nephi Immortal, Aug 13, 2011.

  1. Please confirm if my code is valid with no undefined behavior. When
    you create data members, any built-in types such as char, int are
    always uninitialized until you tell Constructor function to initialize
    them.

    What about two data members: x and y in Z class? Both x and y are
    always initialized automatically with memory address before you call
    Constructor function.

    Reference is like a pointer to set up memory address. The y in Z
    class is always bound to Y class and it can’t be unbound unless you
    change from reference to pointer.

    struct X
    {
    X( int _x, int _y ) : x( _x ), y( _y )
    {
    }

    ~X()
    {
    }

    int x;
    int y;
    };

    struct Y
    {
    Y( X& _x ) : x( _x )
    {
    }

    ~Y()
    {
    }

    X &x; // reference
    };

    struct Z
    {
    Z( int a, int b ) : x( a, b ), y( x ) // initialized y( x )
    {
    }

    ~Z()
    {
    }

    X x;
    Y y;
    };


    int main()
    {
    Z z( 1, 2 );

    return 0;

    }
    Nephi Immortal, Aug 13, 2011
    #1
    1. Advertising

  2. Nephi Immortal

    Ian Collins Guest

    On 08/14/11 03:52 AM, Nephi Immortal wrote:
    > Please confirm if my code is valid with no undefined behavior. When
    > you create data members, any built-in types such as char, int are
    > always uninitialized until you tell Constructor function to initialize
    > them.
    >
    > What about two data members: x and y in Z class? Both x and y are
    > always initialized automatically with memory address before you call
    > Constructor function.


    They aren't initialised with an address, they have an address. They are
    part of the Z object.

    > Reference is like a pointer to set up memory address. The y in Z
    > class is always bound to Y class and it can’t be unbound unless you
    > change from reference to pointer.
    >
    > struct X
    > {
    > X( int _x, int _y ) : x( _x ), y( _y )
    > {
    > }
    >
    > ~X()
    > {
    > }
    >
    > int x;
    > int y;
    > };
    >
    > struct Y
    > {
    > Y( X& _x ) : x( _x )
    > {
    > }
    >
    > ~Y()
    > {
    > }
    >
    > X&x; // reference
    > };
    >
    > struct Z
    > {
    > Z( int a, int b ) : x( a, b ), y( x ) // initialized y( x )
    > {
    > }
    >
    > ~Z()
    > {
    > }
    >
    > X x;
    > Y y;
    > };
    >
    >
    > int main()
    > {
    > Z z( 1, 2 );
    >
    > return 0;
    >
    > }



    --
    Ian Collins
    Ian Collins, Aug 13, 2011
    #2
    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. ce
    Replies:
    1
    Views:
    2,982
  2. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    745
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    585
  5. siddhu
    Replies:
    12
    Views:
    633
    Gianni Mariani
    Jun 6, 2007
Loading...

Share This Page