Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

struct in C++ ......

Posted on 2003-10-26
8
Medium Priority
?
1,287 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

604 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