Slight confusion about why "&UGrabber::Grab" is in permanent storage

I wanted to check that I’ve understood something correctly from this lecture, can you look over the following and see if it sounds right:

We use “::” to access things in permanent storage, and “.” or “->” to access things in Stack/Heap.

When we first bind input to a function in this tutorial, we pass in the following parameters:
“Grab” = the name of the action as defined in project settings
“IE_Pressed” = the input event
“this” = the instance of the class that the function is called on
“&UGrabber::Release” = the location of the function we’re calling, which is based on the memory address of the original UGrabber class definition

I suppose I find it a bit strange that the function we’re calling is in permanent storage and the object that calls the function is “this”, which I think is an instance. I would have thought that “this” would be carrying around its own portable copy of the function. Suppose we added the component at runtime, or instantiated the player at runtime, would we use the same syntax to bind an action?

It does not. The only thing the instance has is the data.

Member functions are essentially syntactic sugar.

class Example
{
public:
    void DoThing();
};

Example E;
E.DoThing();

Is effectively

class Example
{
};

void DoThing(Example*);

Example E;
DoThing(&E);

Yes.

class Example // This is the declaration of the Example class (and also the definition?)
{
public:
    void DoThing(); // This declares the DoThing() function
};

Example E; // This creates an instance of the Example class 
E.DoThing(); // This calls the DoThing() function on the instance, E, of the Example class

class Example //This declares the Example class (and is also the definition?)
{
};

void DoThing(Example*); //This declares the DoThing() function, but I'm unsure what the parameter is
//I'm a bit confused by the " * ". Is this a pointer to an instance of Example?

Example E; //This creates an instance of the Example class
DoThing(&E); //This passes a reference to E into the DoThing() function

As I currently understand the above, the main point seems to be: We declare member functions in the body of a class, but when the program runs, it’s actually passing instances of the class into the function.

I still feel like I could be wildly off with this though :grimacing:

This is the declaration of the Example class (and also the definition?

I would word that the other way around. It’s the definition of the class and also the declaration of it.

This declares the DoThing() function, but I’m unsure what the parameter is
I’m a bit confused by the " * ". Is this a pointer to an instance of Example?

Yes

This passes a reference to E into the DoThing() function

Passes in the address of E. That’s what the invisible first parameter this is on member functions. Also adding const to the end of member functions makes that const. e.g.

void Example::DoThing() const;

Is

void DoThing(const Example*);
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms