I've built an application twice: once in Visual Studio and another time in XCode. One of the libraries I used, GLFW, allows you to use the glfwSetWindowSizeCallback
function to detect resizing of the window.
My window class, Window
, has two private members, width and height. And upon the calling of my callback, window_size_callback
, I wanted the values of width and height to be updated. However, I wanted to do this without the use of setters.
So, I made window_size_callback
a static friend. This solution worked just fine in the Visual Studio compiler; however, XCode returned an error: 'static' is invalid in friend declarations.
window_size_callback
:
void window_size_callback(GLFWwindow* window, int width, int height) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
win->width = width;
win->height = height;
}
glfwGetWindowUserPointer
is used to get the current window instance from outside of the class.
header file:
#include <GLFW/glfw3.h>
class Window {
private:
int m_width;
int m_height;
private:
friend static void window_size_callback(GLFWwindow* window, int width, int height);
}
Without the friend keyword, window_size_callback
is unable to access these members.
Why is VS fine with this and XCode not?
And, how can I get around this without using setters?
Just remove the static
. It makes no sense as I explained in the comments. Here's a snippet that should clear things:
class Window {
private:
int m_width;
int m_height;
private:
friend void window_size_callback(GLFWwindow*, int, int);
};
// as you can see 'window_size_callback' is implemented as a free function
// not as a member function which is what 'static' implies
void window_size_callback(GLFWwindow* window, int width, int height) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
win->width = width;
win->height = height;
}
A friend
function cannot be a static
member of a class. I'm guessing VS allows the syntax as an extension. Don't count on it.
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.