C ++:通过类或实例访问const成员var? [英] C++: access const member vars through class or an instance?

查看:84
本文介绍了C ++:通过类或实例访问const成员var?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,是否有任何理由不通过类实例访问静态成员变量?我知道Java对此不满意,并想知道C ++是否重要。示例:

In C++, is there any reason to not access static member variables through a class instance? I know Java frowns on this and was wondering if it matters in C++. Example:

class Foo {
  static const int ZERO = 0;
  static const int ONE = 1;
  ...
};


void bar(const Foo& inst) {
   // is this ok?
   int val1 = inst.ZERO;
   // or should I prefer:
   int val2 = Foo::ZERO
   ...
};

我有一个额外的第二个问题。如果声明静态双精度型,则必须在某个地方定义它,并且该定义必须重复该类型。为什么必须重复输入类型?
例如:

I have a bonus second question. If I declare a static double, I have to define it somewhere and that definition has to repeat the type. Why does the type have to be repeated? For example:

In a header:
  class Foo {
    static const double d;
  };
In a source file:
  const double Foo::d = 42;

为什么我必须在cpp文件中重复 const double部分?

Why do I have to repeat the "const double" part in my cpp file?

推荐答案

对于第一个问题,除了样式问题(它显然是类变量而且没有关联的对象)之外,Fred Larsen在对问题的评论中,引用了先前的问题。阅读亚当·罗森塔尔(Adam Rosenthal)的回答,这是非常的一个很好的理由,您要对此小心谨慎。 (如果弗雷德将弗雷德作为答案发布,我会投票,但我不能相信这是到期日。我对亚当赞了。)

For the first question, aside from the matter of style (it makes it obvious it's a class variable and has no associated object), Fred Larsen, in comments to the question, makes reference to previous question. Read Adam Rosenthal's answer for very good reason why you want to be careful with this. (I'd up-vote Fred if he'd posted it as answer, but I can't so credit where it's due. I did up-vote Adam.)

关于第二个问题:


为什么我必须在cpp文件中重复 const double部分?

Why do I have to repeat the "const double" part in my cpp file?

您必须重复该类型,主要是作为实现细节:这是C ++编译器解析声明的方式。这对于局部变量也不是完全理想的,C ++ 1x(以前为C ++ 0x)利用 auto 关键字来避免对常规变量重复

You have to repeat the type primarily as an implementation detail: it's how the C++ compiler parses a declaration. This isn't strictly ideal for local variables either, and C++1x (formerly C++0x) makes use of the auto keyword to avoid needing to be repetitive for regular function variables.

所以这:

vector<string> v;
vector<string>::iterator it = v.begin();

可以变成这样:

vector<string> v;
auto it = v.begin();

没有明确的理由说明为什么它也不能与static一起使用,所以在您的情况下:

There's no clear reason why this couldn't work with static as well, so in your case thos:

const double Foo::d = 42;

很可能会变成这个。

static Foo::d = 42;

关键是要有 some 方式将其标识为声明。

The key is to have some way of identifying this as a declaration.

请注意,我说的不是明确原因:C ++的语法是鲜活的传说:极其很难涵盖所有内容它的边缘情况。我不认为上面是模棱两可的,但可能是。如果不是,他们可以将其添加到语言中。告诉他们...对于C ++ 2x:/。

Note I say no clear reason: C++'s grammar is a living legend: it is extremely hard to cover all of its edge cases. I don't think the above is ambiguous but it might be. If it isn't they could add that to the language. Tell them about it ... for C++2x :/.

这篇关于C ++:通过类或实例访问const成员var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆