1

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:

  • function loadTexture, taking const char* and SDL_Renderer* as arguments, and returning void
  • function Init, taking const char* and const Window& as arguments, and returning void
  • class Window, which has function getRenderer, taking no arguments and returning SDL_Renderer*

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.

Community
  • 1
  • 1
MatthewRock
  • 1,071
  • 1
  • 14
  • 30

1 Answers1

6

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());
}
nullptr
  • 11,008
  • 1
  • 23
  • 18
  • Man, I'm dumb... I thought that it disallowed me for some reason to do so, but turns out I forgot to name Window&, and just kept using name that never existed... Thanks for leading me into obvious answer. – MatthewRock Aug 31 '13 at 20:47