简体   繁体   中英

How to initialize member pointer to free function?

I'm trying to create a class with pointer, that can be changed outside of the class. No matter what I do, I either get syntax error or the pointer is simply not initialized I have the following code:

window.h

#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
class window
{
public:
    GLFWwindow* wnd;

    window(int width, int height, const char* title);
    void close();

    void (*update)(window*);
    void (*draw)(window*);

    void run();

    void setDrawFunction(void (*fnptr)(window*));
    void setUpdateFunction(void (*fnptr)(window*));
};

window.cpp

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "window.h"

void default_draw(window* wnd) {
    glClear(GL_COLOR_BUFFER_BIT);
}

void default_update(window* wnd) {
    
}

window::window(int width, int height, const char* title)
{
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    wnd = glfwCreateWindow(width, height, title, NULL, NULL);

    if (wnd == NULL) { glfwTerminate(); return; }

    glfwMakeContextCurrent(wnd);

    if (glewInit() != GLEW_OK) {
        glfwTerminate();
        return;
    }

    //This does not work
    draw = default_draw;
    update = default_update;
}

void window::close() {
    glfwDestroyWindow(wnd);
}

void window::setDrawFunction(void(*fnptr)(window*)) {
    // And so does this
    draw = fnptr;
}

void window::setUpdateFunction(void(*fnptr)(window*)) {
    update = fnptr;
}

void window::run() {
    while (glfwWindowShouldClose(wnd) == 0)
    {
        glClear(GL_COLOR_BUFFER_BIT);

        //update(this);
        draw(this); // This will cause attempt to access 0x0000000000000

        glfwSwapBuffers(wnd);
        glfwPollEvents();
    }

    close();
}

I have tried many things and they didn't work. Am I doing something wrong? I need to use pointers, because this thing above is a part of simple engine wrapped later into python using ctypes.

EDIT: The previous version might have been a little unclear. The part I have problems with is draw function. I;m trying to declare a pointer to free function and then set it in constructor and function. The problem is, that the variable apparently is not set properly.

Instead of using raw function pointers, use std::function:

window.h:

#include <functional>
// [...]

class window {
// [...]

  public:
     std::function<void(window *)> update;
     std::function<void(window *)> draw;
// [...]
}

window.cpp:

void window::setDrawFunction(std::function<void(window *)> fnptr) {
    draw = fnptr;
}

void window::setUpdateFunction(std::function<void(window *)> fnptr) {
    update = fnptr;
}

Learn more by checking https://en.cppreference.com/w/cpp/utility/functional/function .

You can initialize this functions in you class constructor using initialization list like this:

window::window(int width, int height, const char* title)
: update(default_update), draw(default_draw)
{
// [...]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM