?
Solved

struct in C++ ......

Posted on 2003-10-26
8
Medium Priority
?
1,279 Views
Last Modified: 2008-03-03
To C++ Expert,

  I got a code as follows :
------------------
 struct A{
  A(A* a_ptr){ cout << "woof" << endl;}
 } ;

 A* A(A* a_ptr){ cout <<"meow" << endl ;}

 struct A* a1=A(0) ;  //line 1
 struct A* a2 = new struct A(0) ; //line 2
 struct A a3 = A(0) ; //lin3
------------------
 The code does compile and work. But I don't understand :
 
 1. How come the function "A* A(A* a_ptr)" can
     take "0" as  an argument ? i.e. How come line 1
     works ?
 2. How come it is "new struct A(0)" not "new A(0)" ?
     Also, how come the constructor "A(A* a_ptr)"
     can take "0" as an argument ? i.e. How come
     line 2 works ?

    Thanks a lot !!!  
 
0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
8 Comments
 
LVL 1

Accepted Solution

by:
jcwlc earned 300 total points
ID: 9624737
1. A takes a pointer to struct A, the pointer can be null, in c++ 0=NULL
 good practice is to check for null pointers... or at least assert in debug mode. eg: ASSERT(a_ptr)
2. In c++ new allocates memory according to the specified type, not object. A is not a type, struct is... If You typedef struct A... myStructType, then you could write myStructType A = new myStructType.

regards,

0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 9625573
In C++ (as in C), 0 represents NULL.. the type conversion between int and A* is done automatically..

I think in c++, you can use both syntax; you can use "new struct A(0)" and/or"new A(0)".. try it and see if it works.. :)
0
 
LVL 1

Author Comment

by:meow00
ID: 9626964
Hi,

   I tried "struct foo f4 = new f00(0)" and "foo f4 = new f00(0)",
 neither of them compiles. I don't understand that why it works for class but not for struct ....?? Thanks.

meow ......

0
 
LVL 3

Assisted Solution

by:yasser_helmy
yasser_helmy earned 300 total points
ID: 9632170
you need:
struct foo* f4= new foo(0);
new returns a pointer to the structure not the structure itself.
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 10434251
accept an answer please
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question