Solved

struct in C++ ......

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wefewf 2 43
countAbc challenge 9 49
topping2 challenge 13 61
Program to display an alert on Windows Toolbar 2 65
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now