从C#到C++;这个运算符vs::and->; 我对C++是相当新的。< /P> 通过一些现有的C++代码,我看到:和->使用的运算符。问题是,它们之间的区别是什么?什么时候你会使用其中一种
我偶尔在C#中见过::运算符,但我总是使用。要访问成员,请隐式声明作用域。假设这两个是C++中的等价物,我仍然不确定为什么我们使用箭头->运算符,为什么它似乎可以与点互换使用。接线员从C#到C++;这个运算符vs::and->; 我对C++是相当新的。< /P> 通过一些现有的C++代码,我看到:和->使用的运算符。问题是,它们之间的区别是什么?什么时候你会使用其中一种,c#,c++,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 fun
谢谢。
:
是范围运算符。它用于说明您所处的范围:
// 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();
->
运算符用于访问指向的对象(堆或堆栈)的成员
它相当于(*pA).foo()
。C#没有自动变量的概念,因此它不需要解引用运算符(这就是*pA
和pA->
正在做的事情)。由于C#几乎把所有东西都当作一个指针来对待,因此假定取消引用。对于C#中的运算符。。。沿左侧查看符号。有关详细信息,请参阅此。对于C++,请看这里:
A* pA = new A;
a->foo();
A a;
A* pA2 = &a;
pa2->foo();