从 C# 到 C++ .运算符与 :: 和 ->
From C# to C++ The . Operator vs :: and ->
我对C++相当陌生。
通过一些现有的C++代码,我看到了使用的 :: 、. 和 -> 运算符。问题是,它们之间有什么区别,什么时候使用其中一个?
我偶尔在 C# 中看到过 :: 运算符,但我总是使用 . 来访问成员,隐式声明范围。假设这两个是C++中的等价物,我仍然不确定为什么我们使用箭头 -> 运算符以及为什么它似乎可以与点互换使用。算子。
谢谢。
::
是范围运算符。 它用于说明您所处的范围:
// in the A.h file
class A
{
public:
A();
void foo();
};
// in the A.cpp file
#include "A.h"
A::A() // the scope of this function is A - it is a member function of A
{
}
void A::foo() // same here
{
}
void bar() // as written, this function is in the global scope ::bar
{
}
.
运算符用于访问"堆栈"分配变量的成员。
A a; // this is an automatic variable - often referred to as "on the stack"
a.foo();
->
运算符用于访问指向的内容(堆或堆栈)的成员。
A* pA = new A;
a->foo();
A a;
A* pA2 = &a;
pa2->foo();
它相当于(*pA).foo()
. C# 没有自动变量的概念,因此它不需要取消引用运算符(这是 *pA
和 pA->
正在做的事情)。 由于 C# 几乎将所有内容都视为指针,因此假定取消引用。
相关文章:
- 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;未找到