C++"::"和"->"的差异
C++ difference of "::" and "->"
我只是好奇::
和->
在c++中的区别是什么?
目前正在学习c++,因为我想学习openGL和很多使用c++的openGL教程,所以我会选择有很多教程的语言:)
在java
或C#
中,如果您想调用函数或保留函数,只需使用"。",例如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();
}
}
window使用->,而sf使用::
::
是作用域解析操作符,用于引用静态类成员和命名空间元素。
->
是间接引用操作符,用于引用实例指针上的成员方法和字段。
.
是直接引用操作符,用于引用实例中的成员方法和字段。
由于Java没有真正的指针,所以没有必要使用间接的引用操作符
操作符->
用于当你有一个指向对象的指针并且对指针解引用时,即
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;
...
}
参见维基百科
相关文章:
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到