Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8791 > unrolled thread
| Started by | Nephi Immortal <immortalnephi@gmail.com> |
|---|---|
| First post | 2011-08-13 08:52 -0700 |
| Last post | 2011-08-14 08:43 +1200 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.c++
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
| From | Nephi Immortal <immortalnephi@gmail.com> |
|---|---|
| Date | 2011-08-13 08:52 -0700 |
| Subject | Reference to class question |
| Message-ID | <af58ebd4-73e7-4392-99dc-2391f89e2640@hl8g2000vbb.googlegroups.com> |
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;
}
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-14 08:43 +1200 |
| Message-ID | <9ao600FdeuU5@mid.individual.net> |
| In reply to | #8791 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web