Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

struct in C++ ......

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 !!!  
 
ASKER CERTIFIED SOLUTION
Avatar of jcwlc
jcwlc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yasser_helmy
yasser_helmy

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.. :)
Avatar of meow00

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
accept an answer please