之间的区别。和 :: 在 C++对于静态成员?

发布于 2024-11-24 21:58:28 字数 458 浏览 1 评论 0原文

可能的重复:
何时使用点、箭头或双冒号来引用 C++ 中的类成员?

当我尝试使用 Class.Variable 我收到的错误是“变量”左侧的错误必须具有类/结构/联合,当我执行 Class::Variable 时,我没有收到任何错误。尽管在这两种情况下我都通过智能感知获得了 Variable 。在这种情况下, .:: 之间到底有什么不同?

Possible Duplicate:
When do I use a dot, arrow, or double colon to refer to members of a class in C++?

When I try to access my static variable using Class.Variable I get the error that Error left of 'Variable' must have class/struct/union and when I do Class::Variable I get no error. Although in both cases I get Variable through intellisense. What exactly is the different between . and :: in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寄风 2024-12-01 21:58:28

可以通过两种方式访问​​类的静态成员

(a) 使用类的实例 - 使用 . 例如 obj.variable

(b) 不使用类的实例 - 使用 :: 例如class::variable

Static members of a class can be accessed through two ways

(a) With instances of class - Use . e.g. obj.variable

(b) Without instance of class - Use :: e.g. class::variable

鱼忆七猫命九 2024-12-01 21:58:28

. 用于对象 :: 作为类名。

 struct foo {
     int x;
     static int y;
 };

 foo bar;

 bar.x = 10; // ok
 bar.y = 20; // ok - but bad practice

 foo.x = 10; // WRONG foo is class name
 foo.y = 20; // WRONG foo is class name

 foo::x = 10; // WRONG x requires object
 foo::y = 20; // ok

最佳实践:

 bar.x = 10; 
 foo::y = 20;

. is used for objects :: for class names.

 struct foo {
     int x;
     static int y;
 };

 foo bar;

 bar.x = 10; // ok
 bar.y = 20; // ok - but bad practice

 foo.x = 10; // WRONG foo is class name
 foo.y = 20; // WRONG foo is class name

 foo::x = 10; // WRONG x requires object
 foo::y = 20; // ok

best practice:

 bar.x = 10; 
 foo::y = 20;
吲‖鸣 2024-12-01 21:58:28

. 是一个实例引用(例如,在 LHS 上有一个对象)。 :: 是静态引用。 RHS 上有一个类型。

. is an instance reference (e.g. on the LHS there is an object). :: is a static reference. On the RHS there is a type.

_失温 2024-12-01 21:58:28
Class::StaticProperty
InstanceOfClass.Property
Class::StaticProperty
InstanceOfClass.Property
骷髅 2024-12-01 21:58:28

:: 用于类/命名空间范围,但在这种情况下 . 的左侧必须是变量。请注意,这是可行的,这可能就是为什么 Intellisense 对您也适用的原因:

Class x;
doSomething( x.Variable );

the :: is for class/namespace scope, but the left hand side of . must be a variable in this case. Note that this would work, which might be why Intellisense works as well for you:

Class x;
doSomething( x.Variable );
赏烟花じ飞满天 2024-12-01 21:58:28

点运算符也称为“类成员访问运算符”,需要类/结构的实例。

struct X
{
    static int x = 5;
    int y = 3;
}
namespace NS
{
    int z = 1;
}

void foo()
{
    X instance;
    instance.x;  // OK, class member access operator
    X::x;        // OK, qualified id-expression
    instance.y;  // OK
    X::y;        // Not OK, y is not static, needs an instance.
    NS.z;        // Not OK: NS is not an object
    NS::z;       // OK, qualified-id expression for a namespace member.
}

该措辞取自 C++0x FDIS。

The dot operator, also called the "class member access operator", needs an instance of a class/struct.

struct X
{
    static int x = 5;
    int y = 3;
}
namespace NS
{
    int z = 1;
}

void foo()
{
    X instance;
    instance.x;  // OK, class member access operator
    X::x;        // OK, qualified id-expression
    instance.y;  // OK
    X::y;        // Not OK, y is not static, needs an instance.
    NS.z;        // Not OK: NS is not an object
    NS::z;       // OK, qualified-id expression for a namespace member.
}

The wording is taken from C++0x FDIS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文