Solved

struct in C++ ......

Posted on 2003-10-26
8
1,276 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 75 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 75 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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

705 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