struct in C++ ......

Posted on 2003-10-26
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 !!!  
Question by:meow00
  • 3

Accepted Solution

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.



Expert Comment

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.. :)

Author Comment

ID: 9626964

   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 ......


Assisted Solution

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.

Expert Comment

ID: 10434251
accept an answer please

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
recursion example 16 112
index Out OF Range Exception error 4 54
"Black Box" Testing of Control System Software 2 26
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now