www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Instantiating an Object with the Same Object as an Argument?

reply Ron Tarrant <rontarrant gmail.com> writes:
I've come across this a few times in the wrapper code for GtkD 
where one of the constructors for an object takes an argument of 
the same type the constructor produces.

For instance, one of the Adjustment constructors looks like this:

	public this(GtkAdjustment* gtkAdjustment, bool ownedRef = false)
	{
		this.gtkAdjustment = gtkAdjustment;
		super(cast(GObject*)gtkAdjustment, ownedRef);
	}

I have a vague memory of finding out how to use this type of 
constructor from a while back, but I can't remember how. And if I 
did find out, I neglected to take notes, apparently (shame on me) 
but I may also have dreamed it.

And I can't find a cogent example.

At first glance, this form of constructor seems impossible to 
use. I mean, how can I instantiate an object passing the object 
(which doesn't exist) as an argument?

Or is this not actually a constructor?

Or maybe there's some magic going on here that I'd understand if 
I knew D better?

I keep running across this, so I'm hoping someone can give me a 
leg up.
Apr 17 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 17 April 2019 at 14:49:33 UTC, Ron Tarrant wrote:
 For instance, one of the Adjustment constructors looks like 
 this:

 	public this(GtkAdjustment* gtkAdjustment, bool ownedRef
Those aren't the same thing! One is Adjustment, the D wrapper class, and the other is GtkAdjustment*, the C structure pointer. This constructor is for cases when you have an existing object made via the C api and you want the D wrapper to adopt it.
Apr 17 2019
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
btw if you haven't seen my version of the gtk-d docs, check it 
out:

http://gtk-d.dpldocs.info/gtk.Adjustment.Adjustment.html

It does various cross-referencing the official versions don't, 
among other things. You can see the GtkAdjustment there is not 
linked, because it is the C version, whereas references to 
gtk.Adjustment are links to the D classes.
Apr 17 2019
prev sibling parent Ron Tarrant <rontarrant gmail.com> writes:
On Wednesday, 17 April 2019 at 15:05:22 UTC, Adam D. Ruppe wrote:
 On Wednesday, 17 April 2019 at 14:49:33 UTC, Ron Tarrant wrote:
 For instance, one of the Adjustment constructors looks like 
 this:

 	public this(GtkAdjustment* gtkAdjustment, bool ownedRef
Those aren't the same thing! One is Adjustment, the D wrapper class, and the other is GtkAdjustment*, the C structure pointer. This constructor is for cases when you have an existing object made via the C api and you want the D wrapper to adopt it.
Ah! Thanks for clearing that up, Adam. I shall go blush now for not reading more closely.
Apr 17 2019