question



 DEVELOP > c-Plus-Plus > question

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "ÕÅÖ¾¸Õ"
Date: 10 May 2006 01:26:07 AM
Object: question
#include "stdio.h"
#include "conio.h"
class INT
{
public:
int p;
INT(int a):p(a){printf("%d.p= %d\r\n",(int)this,p);}
};
class C
{
public:
INT c;
C():c(0){}
C(int a):c(a){ C(); }
};
void main()
{
C cc(9);
printf("%d.c=%d\r\n",(int)&cc,cc.c.p); //how many is this p £¿9£¿0£¿
getch();
}
look at the result printed:
1245052.p= 9
1244956.p= 0
1245052.c=9
the result of c is 9. why are there to address of p? 1245052.p= 9
1244956.p= 0
.

User: "Jonathan Mcdougall"

Title: Re: question 10 May 2006 01:33:14 AM
=D5=C5=D6=BE=B8=D5 wrote:

#include "stdio.h"

Prefer <cstdio>

#include "conio.h"

Non standard.

class INT

Reserve all-uppercase names to macros.

{
public:
int p;
INT(int a):p(a){printf("%d.p=3D %d\r\n",(int)this,p);}

Some formatting would be appreciated. You don't need to write "\r\n" to
get a newline, just "\n" will do.

};
class C
{
public:
INT c;
C():c(0){}
C(int a):c(a){ C(); }

I'm not sure this does what you expect. It dosen't "call" C's default
constructor (which means you cannot in C++ "chain" constructor calls as
Java or C# does), it creates an unnamed object of type C, which is
destroyed right at the semicolumn. This is the same as
C(int a)
: c(a)
{
C temp;
}

};
void main()

main() returns an int. Always.

{
C cc(9);
printf("%d.c=3D%d\r\n",(int)&cc,cc.c.p); //how many is this p =A3=BF9=A3=

=BF0=A3=BF

getch();

getch() is non standard.

}

look at the result printed:

1245052.p=3D 9
1244956.p=3D 0
1245052.c=3D9

the result of c is 9. why are there to address of p? 1245052.p=3D 9
1244956.p=3D 0

The second "p" you see is the one created in C's constructor. Remove
the "C()" bit and the "problem" will go away.
Jonathan
.
User: "???"

Title: thanks Re: question 10 May 2006 02:19:39 AM
thanks
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER