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
 
J

Jonathan Mcdougall

ÕÅÖ¾¸Õ said:
#include "stdio.h"

Prefer said:
#include "conio.h"

Non standard.
class INT

Reserve all-uppercase names to macros.
{
public:
int p;
INT(int a):p(a){printf("%d.p= %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=%d\r\n",(int)&cc,cc.c.p); //how many is this p £¿9£¿0£¿
getch();

getch() is non standard.
}

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

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


Jonathan
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top