www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how use * in D just like C++

reply liumao <loveyou12300liumao 163.com> writes:
for example
     class A;
     {
         void doSomething() {}
     }

     A *a = cast(A *) new A;

     then I call a.doSomething(); my program "Segmentation fault"



how could it happened? and how i use it correctly?


3q, please tell me, 3q 3q 3q!!!!!
Oct 26 2015
parent Jakob Ovrum <jakobovrum gmail.com> writes:
On Tuesday, 27 October 2015 at 05:36:58 UTC, liumao wrote:
 for example
     class A;
     {
         void doSomething() {}
     }

     A *a = cast(A *) new A;

     then I call a.doSomething(); my program "Segmentation fault"



 how could it happened? and how i use it correctly?


 3q, please tell me, 3q 3q 3q!!!!!
`A` on its own is already a reference type. `A*` is a pointer to a class reference, i.e. a pointer to a pointer, like `A**` in C++. http://wiki.dlang.org/Coming_From/C_Plus_Plus#Slicing_problem Please use D.learn for questions like this in the future.
Oct 26 2015