Example for using LiquidMenu inside classes #87
-
The examples provided are not useful for a real-case scenario. Usually, any application is organized in classes and it's not a good approach to put everything outside. My limited knowledge of C++ does not allow me to fix the syntax. I provide here some code (far from to be elegant, but more usable than the examples) and I kindly ask you a minimal example to show how to integrate this great library in a class. main.cpp
myclass.h
myclass.cpp
This code does not compile:
But I don't want to use static members, otherwise I cannot access to the other variables or functions.
it compiles but when I call the function:
it returns 0, so the actual function is not called. Please, can you show me how to deal with classes? Furthermore, in my menu I have dozens of vars to set. I really don't want to create a lot of functions to increase or decrease each single var! Is there a way to retrieve the variable being edited inside the callback? In this way I can set the same function for all the variables (given they are of the same type) ending in a more elegant a re-usable code. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello, @Mark-81. The problem is that the library holds pointers to functions and not pointers to member functions of your specific class. That's why you must trick the compiler by casting your member function address to a normal function address. You can read more about member function pointers here. On your point about using classes for everything. If you're coming from Java or C#, it's not bad practice to have declarations outside of classes in C++. If you're new to coding, don't take guidelines as rules (and I'm pretty sure there isn't such a guideline). If you ask me, one of the best advices is to not write needlessly complex code.
Using a single callback function and figuring out who called it, will make that function huge, which doesn't tick the elegant box. A workaround would be to group your callback functions inside a namespace, spanning multiple files or in a separate one, just for callback functions. But if you still want to do it in a single callback function, you can use Hope this helps. |
Beta Was this translation helpful? Give feedback.
Hello, @Mark-81.
The problem is that the library holds pointers to functions and not pointers to member functions of your specific class. That's why you must trick the compiler by casting your member function address to a normal function address. You can read more about member function pointers here.
On your point about using classes for everything. If you're coming from Java or C#, it's not bad practice to have declarations outside of classes in C++. If you're new to coding, don't take guidelines as rules (and I'm pretty sure there isn't such a guideline). If you ask me, one of the best advices is to not write needlessly complex code.