C ++与“::”和“ - >”的区别

时间:2013-05-27 12:16:22

标签: c++

我只是好奇C ++中::->的区别是什么?

目前正在学习C ++,因为我想用c ++学习openGL和openGL中的大量教程,所以我会选择有很多教程的语言:)

javaC#中如果要调用函数或保留函数,只需使用“。”例如text1.getText();如果您要将其转换为C++那将是text1->getText()吗?什么叫他们?标题不合适。如果->等于“。”在java然后使用“::”?我相信有很多问题像我一样,但我不知道该怎么称呼他们,所以我无法获得准确的信息。顺便说一句,我发现这个::在使用sfml时会想到。

这是一个例子

if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }

void renderingThread(sf::Window* window)
    {
        // activate the window's context
        window->setActive(true);


        // the rendering loop
        while (window->isOpen())
        {
            // draw...

            // end the current frame -- this is a rendering function 

(it requires the context to be active)
                window->display();
            }
        }

窗口使用 - >而sf使用::

2 个答案:

答案 0 :(得分:11)

::范围解析运算符,用于引用静态类成员和名称空间元素。

-> 间接 引用运算符,用于引用实例指针上的成员方法和字段。

. 直接 引用运算符,用于引用实例上的成员方法和字段。

由于Java没有真正的指针,因此间接引用运算符没有用。

答案 1 :(得分:4)

当你有一个指向对象的指针和取消引用指针时,使用运算符->,即

string* str = new string("Hello, World");
const char* cstr = str->c_str();

,而

string str("Hello World");
const char* cstr = str.c_str();

用于直接引用成员。

::是“范围运营商”。您可以使用它来呼叫班级的static成员或引用namespace中的成员。

namespace X{
 int a;
}

int main() {
...
X::a = 4;
...
}

请参阅Wikipedia

相关问题