Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass non static member function into glutDisplayFunc

Tags:

c++

static

opengl

I have a class which has functionality to initialise opengl and run it in separate thread.

My problem is: openGL callbacks such as glutDisplayFunc, glutMotionFunc etc accepts void (*f) void, and I cannot pass class member function.

ways around. 1) I can declare member function as static, but in this case I need to declare all used member variables as static, and end up declaring a whole class as a static.

2) I can use some stand alone function and declare my object as global, but its too bad.

I wonder if there are some ways around so I dont need to make my opengl class static ?? (using c++)

like image 882
kirbo Avatar asked Apr 19 '10 20:04

kirbo


People also ask

How can one refer to a non static member?

You either have to use a pointer to member, or a free function.

What is non static member function in C++?

A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)

Can a static member function call non static member function of a class?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .

What happens if non static members?

What happens if non static members are used in static member function? Explanation: There must be specific memory space allocated for the data members before the static member functions uses them. But the space is not reserved if object is not declared.


2 Answers

You'll need to create a "thunk" or "trampoline": C API function callbacks into C++ member function code

like image 128
Fred Larson Avatar answered Oct 27 '22 00:10

Fred Larson


Since the callbacks don't take any parameters, there is not much you can do. One possibility is to create a namespace with functions and a static data member which is your current class. The functions would just forward the function calls to the existing class. This is just basically a slightly cleaner version of your #2.

namespace GLForwader
{
    static MyGLClass* = new myGlClass();

    void glutDisplayFunc() {myGlClass->glutDisplayFunc();}
    void glutMotionFunc() {myGlClass->glutMotionFunc();}
}
like image 21
KeithB Avatar answered Oct 26 '22 23:10

KeithB