Reputation: 1091
I am looking for a way of calling member function without creating instance of class, and I don't mean static function.
Here's the situation:
//Texture.cpp
#include "Window.hpp"//Window included
void Texture::Init(const char* path, const Window&)
{
loadTexture(path, Window.getRenderer();
}
void Texture::loadTexture(const char* path, SDL_Renderer* renderer)
{
//code...
}
Window has a member function SDL_Renderer* getRenderer(). However, I can't use it in this situation, because there is no instance of Window created.
I came across this question, and if I had to find a way by myself, I would do the same thing as the guy did: creating static function. However, this looks like going around a problem for me. The answer with using std::function and std::bind looked good to me, but I can't figure out how to use it. Could anyone help me please?
I have:
Could anybody help me please, with explanation, so I could do the same thing by myself next time I encounter this problem?
Thanks in advance.
Upvotes: 1
Views: 2153
Reputation: 11058
You cannot execute a non-static method without an instance (neither std::function nor anything else would allow to do this).
Usually non-static methods access some instance data. You have to provide these data (i.e. the instance).
In your case I suppose that a Renderer must know something about window it renders.
But why do you want to call getRenderer without an instance? You have an instance: const Window&
is an instance. So just use the second argument:
void Texture::Init(const char* path, const Window& wnd)
{
loadTexture(path, wnd.getRenderer());
}
Upvotes: 6