C++ 非静态数据成员的无效使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9590265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid use of non-static data member
提问by mahmood
For a code like this:
对于这样的代码:
class foo {
protected:
int a;
public:
class bar {
public:
int getA() {return a;} // ERROR
};
foo()
: a (p->param)
};
I get this error:
我收到此错误:
invalid use of non-static data member 'foo::a'
currently the variable a
is initialized in the constructor of foo
.
当前变量a
是在 的构造函数中初始化的foo
。
if I make it static, then it says:
如果我让它静态,那么它会说:
error: 'int foo::a' is a static data member; it can only be initialized at its definition
However I want to pass a value to a
in the constructor.
What is the solution then?
但是我想a
在构造函数中传递一个值。那有什么解决办法呢?
采纳答案by ruakh
In C++, unlike (say) Java, an instance of a nested class doesn't intrinsically belong to any instance of the enclosing class. So bar::getA
doesn't have any specific instance of foo
whose a
it can be returning. I'm guessing that what you want is something like:
在 C++ 中,与(比如说)Java 不同,嵌套类的实例本质上不属于封闭类的任何实例。因此,bar::getA
没有任何具体的实例foo
,其a
可以返回。我猜你想要的是这样的:
class bar {
private:
foo * const owner;
public:
bar(foo & owner) : owner(&owner) { }
int getA() {return owner->a;}
};
But even for this you may have to make some changes, because in versions of C++ before C++11, unlike (again, say) Java, a nested class has no special access to its enclosing class, so it can't see the protected
member a
. This will depend on your compiler version. (Hat-tip to Ken Wayne VanderLinde for pointing out that C++11 has changed this.)
但即便如此,您也可能需要进行一些更改,因为在 C++11 之前的 C++ 版本中,与(再次说)Java 不同,嵌套类对其封闭类没有特殊访问权限,因此它看不到protected
会员a
。这将取决于您的编译器版本。(感谢 Ken Wayne VanderLinde 指出 C++11 已经改变了这一点。)
回答by Ken Wayne VanderLinde
In C++, nested classes are not connected to any instance of the outer class. If you want bar
to access non-static members of foo
, then bar
needs to have access to an instance of foo
. Maybe something like:
在 C++ 中,嵌套类不连接到外部类的任何实例。如果要bar
访问 的非静态成员foo
,则bar
需要访问 的实例foo
。也许是这样的:
class bar {
public:
int getA(foo & f ) {return foo.a;}
};
Or maybe
或者可能
class bar {
private:
foo & f;
public:
bar(foo & g)
: f(g)
{
}
int getA() { return f.a; }
};
In any case, you need to explicitlymake sure you have access to an instance of foo
.
在任何情况下,您都需要明确确保您可以访问foo
.
回答by Kerrek SB
The nested class doesn't know about the outer class, and protected
doesn't help. You'll have to pass some actual reference to objects of the nested class type. You could store a foo*
, but perhaps a reference to the integer is enough:
嵌套类不知道外部类,protected
也没有帮助。您必须传递一些对嵌套类类型对象的实际引用。您可以存储 a foo*
,但也许对整数的引用就足够了:
class Outer
{
int n;
public:
class Inner
{
int & a;
public:
Inner(int & b) : a(b) { }
int & get() { return a; }
};
// ... for example:
Inner inn;
Outer() : inn(n) { }
};
Now you can instantiate inner classes like Inner i(n);
and call i.get()
.
现在您可以实例化内部类,例如Inner i(n);
并调用i.get()
.
回答by Alecs
You try to access private member of one class from another. The fact that bar-class is declared within foo-class means that bar in visible only inside foo class, but that is still other class.
您尝试从另一个类访问一个类的私有成员。bar-class 在 foo-class 中声明的事实意味着 bar 仅在 foo 类中可见,但这仍然是其他类。
And what is p->param?
什么是 p->param?
Actually, it isn't clear what do you want to do
实际上,不清楚你想做什么