Just Game Dev Just Game Dev - 24 days ago 11
C++ Question

How to access a function from a class?

How do you access a function from a class, outside of that class?

Example:

int main(int argc, char *argv[])
{
class ImageLoading
{

void Loading()
{
cout << 5 <<endl;
}

};

Loading();

return 0;
}

Answer

A standard function defined in a class is an instance class. It can be used only on an instance, i.e. a created object. To do that, you need to create an ImageLoading class instance:

ImageLoading instance;
instance.Loading();

It is possible too to create functions associated to the class and not to its instances. For that, you need to add the keyword static in the function definition

static void Loading()
{
    cout << 5 << endl;
}

Then to use it, you need to tell that it is the static class defined in ImageLoading. The syntax is ClassName::MethodName() so:

ImageLoading::Loading();
Comments