Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8797
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Reference to class question |
| Date | 2011-08-14 08:43 +1200 |
| Message-ID | <9ao600FdeuU5@mid.individual.net> (permalink) |
| References | <af58ebd4-73e7-4392-99dc-2391f89e2640@hl8g2000vbb.googlegroups.com> |
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
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
Reference to class question Nephi Immortal <immortalnephi@gmail.com> - 2011-08-13 08:52 -0700 Re: Reference to class question Ian Collins <ian-news@hotmail.com> - 2011-08-14 08:43 +1200
csiph-web