Don't understand something about arguments that passed to the BindAxis function

In this line of code

PlayerInputComponent->BindAxis("MoveForward", this, &APawnTank::CalculateMoveInput);

Why are we passing a reference and why do we need to specify a namespace? Otherwise it wont compile, but why? Why cant we just pass an adress?

—Edit—: i realised my mistake about 1 thing: & - isn’t a reference, we are asking for adress, but an adress of what? As far as i remember if we write a function name without brackets then we get the function’s adress (correct me if i am wrong), but what will we get by writing &FunctionName i am not sure, so the question still stands.

—Edit2—:
So i decided to play with the code to see if i can recreate something similar and i did this

#include <iostream>
using namespace std;

class CLS1
{
public:
    void func1()
    {
        cout << "Hello" << endl;
    }

    void func2()
    {
        //cout << func1 << endl; wont compile
        //cout << &func1 << endl; wont compile
        //cout << CLS1::func1 << endl; wont compile
        cout << &CLS1::func1 << endl;
    }
};

int main()
{
    CLS1 a;
    a.func2();
}

What i got from this is that you cant just get an adress of a method (function that is located inside a class) inside another method of the same class. I am guessing that this is because this adress doesnt exist until you create an instance of a class, but i may be wrong. But still i am not sure how & and :: are fixing this issue.

Also the outout of this code is: 1
Why?

That is incorrect. You need & for pointer to member functions which are a C++ feature (C doesn’t have classes) whereas pointer to (non-member) functions are a feature inherited from C and the & isn’t required but you can still add it.

Without the class scope you’re trying to get the address of a non-member function (such a non-member function not existing is not the only reason as to why it doesn’t compile). e.g.

class Example
{
    void Foo(); // 1
};

void Foo(); // 2

&Example::Foo; // address of 1 and the & is required.
&Foo; // address of 2 but the & isn't required.

Because there isn’t an overload for pointer to member functions however pointers (including to functions, and member functions) are implicitly convertible to bool so it chose the overload for bool

std::ostream& operator<<(std::ostream&, bool);

which will always be true (or it doesn’t compile due to not existing)

FAQ from ISO C++:
Standard C++

Thank you very much for your fast and informative reply!

1 Like

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

Privacy & Terms