
C++ 中 :: 和 : 的区别
在 C++ 编程语言中,双冒号 (::) 和 单冒号 (:) 运算符具有不同的用法和含义。
双冒号 (::)
双冒号 (::) 运算符用于以下场景:
立即学习“C++免费学习笔记(深入)”;
namespace outer {
int x = 10;
}
namespace inner {
void printX() {
std::cout << outer::x << std::endl;
}
}
class MyClass {
public:
static int numInstances = 0;
static void printNumInstances() {
std::cout << numInstances << std::endl;
}
};
单冒号 (:)
单冒号 (:) 运算符用于以下场景:
class Base {
public:
int x;
};
class Derived : public Base {
public:
Derived(int x) : x(x) {}
};
int x = 10; double y = static_cast<double>(x);
总结
双冒号 (::) 用于命名空间作用域解析和类静态成员访问,而单冒号 (:) 用于基类初始化和隐式类型转换。
上面的文章就是c++中::和:的区别的内容了,文章的版权归原作者所有,如有侵犯您的权利,请及时联系本站删除,更多相关::的资讯,请关注收藏西西下载站。