VST in C++: 'this' being used in CHorizontalSlider

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

Hi - I've been basing a VSTGUI slider on some template code, and don't quite understand how it works. Especially this bit...

Code: Select all

GainFader = new CHorizontalSlider (size, this, kGain, minPos, maxPos, hFaderHandle, hFaderBody, point);
Ok, I'm guessing this allocates the memory for the slider, and I've looked at the VST documentation for what each of the parameters do. One that puzzles me though is the second parameter, which the template has used 'this' for. I know this parameter is the 'listener' and is the "object responsible of handling events in this control".

Can anyone explain what's going on? What is 'this' being used to do here? Thanks again!

Post

The listener parameter is the object that receives, for example, valueChanged() calls when the slider's value changes.

The slider control itself can't do anything useful except change the way it draws when its value is changed; it needs an object to notify. So that line of code says something like "go be a slider, call my valueChanged() method when something interesting happens."
Image
Don't do it my way.

Post

Brilliant - That makes a lot of sense! Thanks Borogrove... oh, but I'm still a tad unsure of what 'this' is doing. I know it points "to the object for which the member function is called" (MS). Errrr, sorry to be dim, but how does that work in this sense?

Post

How familiar are you with object oriented programming? The code which is creating this new slider is located in your editor class - it's a "member function" of the editor. That means that when the code is run, it has a specific instance of the editor in mind (in principle, you can create ten instances of the editor, run the same code on each of them, but each has its own set of data). The 'this' pointer refers to the specific instance of the editor that's running.

So the code says "go be a slider, and call *me* (i.e. 'this') when something happens, don't call any of those other 9 editors."
Image
Don't do it my way.

Post

Right - I understand. I got a tad confused with Microsoft's explanation of 'this', but what you said makes a lot of sense. Thanks again!

Post

As Borogrove says, basically, the 'this' pointer is the pointer to the data for the object you are dealing with.

e.g.

Code: Select all

class A {
   bool compare_ptr(A* a) { return (this == a); }
};

A a;
A b;
a.compare_ptr(&a); // returns 'true'
a.compare_ptr(&b); // returns 'false'
A little bit more is happening in your situation, because 'this' refers to your editor class, but the CHorizontalSlider constructor takes a pointer to a CControlListener.

If you look at the header file of your editor class, you'll see that it inherits from CControlListener.

e.g:

Code: Select all

class ScopeEditor : 
	public AEffGUIEditor, 
	public CControlListener  
So in this case, ScopeEditor is an AEffGUIEditor, and it also is a CControlListener.

therefore, for the ScopeEditor class, 'this' can be used anywhere a ScopeEditor*, a CControlListener* or an AEffGUIEditor* (or AEffEditor* which AEffGUIEditor inherits from).

When you use 'this' in the CHorizontalSlider constructor, there is an implicit up-cast, where the a pointer to an instance of the editor class is converted to a pointer of CControlListener without you explicitly having to write any code to get it to happen.
The compiler needs to work a little bit of magic here and adjust the address of the pointer, because multiple inheritance is involved.

Post Reply

Return to “DSP and Plugin Development”