Thread overview
Syntax for Pointer to Class
Jan 25, 2019
Q. Schroll
Jan 25, 2019
H. S. Teoh
Jan 25, 2019
Q. Schroll
Jan 25, 2019
rikki cattermole
January 25, 2019
Say I have a class C and I want a pointer to a C handle.

I tried the following pieces of syntax:

  C* obj = new C(); // gives me a C
  C* obj = new C*(); // gives me a C**
  C* obj = C*(); // refuses to compile

Is it even possible? This sounds so newbie...
I know it's fairly simple with structs: S() gives me a struct instance, new S() gives me a ptr to a struct instance.
January 25, 2019
On Fri, Jan 25, 2019 at 08:12:33PM +0000, Q. Schroll via Digitalmars-d-learn wrote:
> Say I have a class C and I want a pointer to a C handle.
> 
> I tried the following pieces of syntax:
> 
>   C* obj = new C(); // gives me a C
>   C* obj = new C*(); // gives me a C**
>   C* obj = C*(); // refuses to compile
> 
> Is it even possible? This sounds so newbie...
> I know it's fairly simple with structs: S() gives me a struct
> instance, new S() gives me a ptr to a struct instance.

Classes in D are inherently by-reference, meaning that if you have a class:

	class C { ... }

then the type `C` is implicitly a pointer to the class instance. Of course, this is hidden by the language syntax so most of the time you don't have to care that it's a pointer, just that it has reference semantics.

If you want the raw pointer to the class instance, just cast the reference to void*, for example:

	C c = new C(...);
	void* ptr = cast(void*) c;

Note that taking the address of `C` will actually give you a pointer to the reference, not the pointer to the class instance itself. For that to be valid, you'll need to store your class reference somewhere first, since it's invalid to take the address of an rvalue:

	C c = new C(...);
	C* ptrToRef = &c;

	// And you'll need to explicitly dereference the pointer to get
	// to the class instance:
	(*ptrToRef).method(...);

Be warned that the pointer will become invalid when the reference `c` goes out of scope, even if the object itself is still live (via another reference), because the pointer is pointing to the class reference rather than the actual object.


T

-- 
Once the bikeshed is up for painting, the rainbow won't suffice. -- Andrei Alexandrescu
January 25, 2019
On Friday, 25 January 2019 at 20:31:29 UTC, H. S. Teoh wrote:
> On Fri, Jan 25, 2019 at 08:12:33PM +0000, Q. Schroll via Digitalmars-d-learn wrote:
>> Say I have a class C and I want a pointer to a C handle.
>
> Note that taking the address of `C` will actually give you a pointer to the reference, not the pointer to the class instance itself.

I know. This is precisely the thing I asked for: A pointer to a handle. I called it handle for exactly this reason.

> For that to be valid, you'll need to store your class reference somewhere first, since it's invalid to take the address of an rvalue

Yes. `&new C(...)` looked to wrong for me to even consider trying.

> 	C c = new C(...);
> 	C* ptrToRef = &c;

So the answer is no. At least not that simple.

> Be warned that the pointer will become invalid when the reference `c` goes out of scope, even if the object itself is still live (via another reference), because the pointer is pointing to the class reference rather than the actual object.

That's kind of obvious if you get remembered, but thanks. I wasn't considering this as I wanted the handle to live on the heap anyway --- the same way for a struct `S`, the pointer pointed to by `S** ptr = new S*(new S(...));` lies on the heap.

I'll use

    C* ptr = &[ new C(...) ][0];

for now. It looks ugly, but it does the job.
January 26, 2019
On 26/01/2019 9:52 AM, Q. Schroll wrote:
>      C* ptr = &[ new C(...) ][0];

C* ptr = [ new C(...) ].ptr;

Should work.