C++ 非静态与静态函数和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2285915/
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
non-static vs. static function and variable
提问by skydoor
I have one question about static and non-static function and variable.
我有一个关于静态和非静态函数和变量的问题。
1) non-static function access static variable.
1) 非静态函数访问静态变量。
It's OK!
没关系!
class Bar
{
public:
static int i;
void nonStaticFunction() {
Bar::i = 10;
}
};
int Bar::i=0;
2) non-static function access non-static variable
2)非静态函数访问非静态变量
Definitely OK!
绝对OK!
3) static function access static variable&funciton
3) 静态函数访问静态变量&函数
Definitely OK!
绝对OK!
4) static function access non-static function
4)静态函数访问非静态函数
It's OK
没关系
class Bar
{
public:
static void staticFunction( const Bar & bar)
{
bar.memberFunction();
}
void memberFunction() const
{
}
}
5) static function access non-static variable
5)静态函数访问非静态变量
It's OK or not OK? I am puzzled about this!
好还是不好?对此我很疑惑!
How about this example
这个例子怎么样
class Bar
{
public:
static void staticFunction( Bar & bar)
{
bar.memberFunction();
}
void memberFunction()
{
i = 0;
}
int i;
};
回答by Doug T.
static function access non-static variable
It's OK or not OK? I am puzzled about this!
静态函数访问非静态变量
好还是不好?对此我很疑惑!
When called, a static function isn't bound to an instance of the class. Class instances (objects) are going to be the entities that hold the "non-static" variables. Therefore, from the static function, you won't be able to access them without actually being passed or storing elsewhere a specific instance to operate on.
调用时,静态函数未绑定到类的实例。类实例(对象)将成为持有“非静态”变量的实体。因此,从静态函数中,如果没有实际传递或在别处存储要操作的特定实例,您将无法访问它们。
So yes, the code in your last example is valid, because you are passed in an instance. However, you could not do:
所以是的,你最后一个例子中的代码是有效的,因为你是在一个实例中传递的。但是,您不能这样做:
static void staticFunction()
{
// error, this function is static, and is therefore
// not bound to a specific instance when called
i = 5;
}
回答by skydoor
Static means this is independent of a particular instance of the class. Static methods don't have access to the this
pointer. That is the reason you need to call them using the class name.
静态意味着这独立于类的特定实例。静态方法无权访问this
指针。这就是您需要使用类名来调用它们的原因。
When you call the Static method, you might not even have any instance of the class defined.
当您调用静态方法时,您甚至可能没有定义该类的任何实例。
non-static means implies an instance, and could be different with different instances.
非静态意味着意味着一个实例,并且可能因不同的实例而异。
So, basically, it does not make sense to access non-static members from static methods.
所以,基本上,从静态方法访问非静态成员是没有意义的。
回答by Narendra N
For this, you need to understand what is static.
为此,您需要了解什么是静态的。
Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. They will have a class scope and does not bound to an instance of the class.
与非静态数据成员相反,静态数据成员对于整个类只存在一次,非静态数据成员单独存在于类的每个实例中。它们将有一个类范围并且不绑定到类的实例。
To access static member of the class, we use the format as below ::
要访问类的静态成员,我们使用如下格式:
if you have created 10 objects of a class. Assume, you were able to access the non-static variable in the static member of the class, When the static function is called, which object's member it needs to change?
如果您创建了一个类的 10 个对象。假设您能够访问类的静态成员中的非静态变量,那么在调用静态函数时,它需要更改哪个对象的成员?
回答by Pace
It's not ok. Static functions are accessible without having an instance of a class and thus can't access information that you would need an instance to determine.
不行。静态函数无需类的实例即可访问,因此无法访问需要实例来确定的信息。
For example, you don't need a car to know how many wheels it has, blueprints for a general car would suffice (that could be static information) but you can't tell what color the car is unless you're referring to a specific car (that information needs a specific instance of an object.)
例如,您不需要汽车知道它有多少个轮子,一般汽车的蓝图就足够了(这可能是静态信息)但是除非您指的是汽车,否则您无法分辨汽车是什么颜色特定汽车(该信息需要对象的特定实例。)