C 语言中 :: 的含义
在 C 语言中,"::" 运算符表示范围解析运算符,它有两种主要用途:
1. 命名空间作用域解析
当类或结构声明在不同的命名空间中时,可以使用 "::" 来指定该类的全限定名。例如:
立即学习“C语言免费学习笔记(深入)”;
<code class="c">namespace foo {
class MyClass {
// ...
};
}
int main() {
foo::MyClass myObject; // 指向命名空间 foo 中的 MyClass 类
return 0;
}</code>2. 成员访问
"::" 也可用于从结构体或类的对象访问其成员。例如:
<code class="c">struct Point {
int x;
int y;
};
int main() {
Point point;
point.x = 10;
point.y = 20;
return 0;
}</code>在上面的示例中,"point.x" 和 "point.y" 使用 "::" 运算符来访问 Point 结构体的成员。
