Sign up

Instantiating an Object with the Same Object as an Argument?

Hi Mike,

I've come across this a few times in the wrapper code where one of the constructors for an object takes an argument of the same type is the constructor produces. For instance, one of the Adjustment constructors looks like this:

	/**
	 * Sets our main struct and passes it to the parent class.
	 */
	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 I can't find a sample file in GtkD. Sadly, I also seem to have neglected to take notes (shame on me). At first glance, it seems impossible to use. I mean, how can I instantiate an object passing itself as an argument if it doesn't yet exist?

Or is not actually a constructor?

I keep running across this, so I'm hoping you can unbend my brain.

Re: Instantiating an Object with the Same Object as an Argument?

On 17-04-2019 15:02, Ron Tarrant wrote:

Hi Mike,

I've come across this a few times in the wrapper code where one of the constructors for an object takes an argument of the same type is the constructor produces. For instance, one of the Adjustment constructors looks like this:

	/**
	 * Sets our main struct and passes it to the parent class.
	 */
	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 I can't find a sample file in GtkD. Sadly, I also seem to have neglected to take notes (shame on me). At first glance, it seems impossible to use. I mean, how can I instantiate an object passing itself as an argument if it doesn't yet exist?

Or is not actually a constructor?

I keep running across this, so I'm hoping you can unbend my brain.

This constructor is used to instantiate a GtkD class given a GTK
struct/pointer. When a GtkD function returns a class the C function it
wraps will return a pointer to a GTK struct.

In this case gtk.Adjustment.Adjustment is the GtkD class, and
GtkAdjustment* is a pointer to a GTK struct.

In most cases this is "hidden" by ObjectG.getDObject because we usually
want to return an existing instance if there is one.

Re: Instantiating an Object with the Same Object as an Argument?

On Wed, 17 Apr 2019 19:32:10 +0200, Mike Wey wrote:

This constructor is used to instantiate a GtkD class given a GTK
struct/pointer. When a GtkD function returns a class the C function it
wraps will return a pointer to a GTK struct.

In this case gtk.Adjustment.Adjustment is the GtkD class, and
GtkAdjustment* is a pointer to a GTK struct.

In most cases this is "hidden" by ObjectG.getDObject because we usually
want to return an existing instance if there is one.

Thanks, Mike. I meant to come back and let you know that Adam Ruppe straightened me out on this earlier in the day. Then I got sleepy. :)