简体   繁体   中英

What does ::(member method name) do in c++

I'm an starter at c++, and I'm trying to develop a simple OpenGL application. Looking at some code on the internet I found this:

::glutDisplayFunc(myPixmap::drawCallback);

Ignore the function itself, i just wanted to know what that line of code does. Does it call the function? I know it's probably a silly question, but I can't find the answer

:: is the scope resolution operator.

If a container (namespace or class) name appears before it, it causes the compiler to only look inside that container for the specified identifier. This is the way to refer to static members of a class from outside the class.

If it appears first, without a name in front, it means to look in the global namespace.

Your example code appears to be contain both usages. That line of code calls ::glutDisplayFunc . But the other function, myPixmap::drawCallback , isn't called. It's address is saved for later.

Yes, it calls the function. The leading "::" just means the function must be found in the global namespace. For example:

namespace X { 
    void whatever() {}
};

void whatever() {}

int main() { 
    whatever(); // calls the global function
    ::whatever(); // also calls global function
    X::whatever(); // calls the function in the namespace
    return 0;
}

Although whatever (with no scope resolution) calls the global function in this case , that depends on context -- in a different context, it could call a function in a namespace instead. Using the leading :: ensures that only the global function can be called, not one in another namespace, regardless of context.

glutDisplayFunc sets the display callback for the current window. When GLUT determines that the normal plane for the window needs to be redisplayed, the display callback for the window is called. Before the callback, the current window is set to the window needing to be redisplayed and (if no overlay display callback is registered) the layer in use is set to the normal plane. The display callback is called with no parameters.

In your case, the myPixmap::drawCallback is most likely a static method function, taking no parameters,

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