在 C++ 中初始化静态指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10620408/
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
Initializing a static pointer in C++
提问by Yassir Ennazk
I have a class with a static member that's a pointer like so :
我有一个带有静态成员的类,它是一个像这样的指针:
animation.h
动画.h
class Animation
{
public:
Animation();
static QString *m;
};
animation.cpp
动画.cpp
#include "animation.h"
QString* Animation::m = 0;
Animation::Animation()
{
}
When I try to initialize that 'm' pointer from another class like so :
当我尝试从另一个类初始化“m”指针时:
Animation::m = new QString("testing");
It works.
有用。
But when I do it this way :
但是当我这样做时:
QString x("Testing");
Animation::m = &x;
The program crashes.
程序崩溃。
What is wrong with this second method ?
第二种方法有什么问题?
Also I would like to have that static pointer as private so I can make static getter and setter functions to it. The setter should use the second method as the 'x' will come in a parameter so I'm stuck.
此外,我希望将该静态指针设为私有,以便我可以为其创建静态 getter 和 setter 函数。setter 应该使用第二种方法,因为 'x' 将出现在一个参数中,所以我被卡住了。
Thanks for any help!
谢谢你的帮助!
采纳答案by Luchian Grigore
I bet it's not crashing on that line, but afterwards.
我敢打赌它不会在那条线上崩溃,而是在那之后。
The problem is that you're taking the address of a variable located in automatic memory, and probably try to access it afterwards. The variable x
will be destroyed when it's scope ends, but Animation::m
will still point to that memory (memory you no longer own after x
went out of scope). This results in undefined behavior.
问题是您正在获取位于自动内存中的变量的地址,然后可能会尝试访问它。该变量x
将在其作用域结束时被销毁,但Animation::m
仍将指向该内存(x
超出范围后您不再拥有的内存)。这会导致未定义的行为。
Just like the following would be illegal:
就像以下内容是非法的:
int* x = NULL;
{
int k = 3;
x = &k;
}
*x = 4;
Workaroundassign to the value, not the pointer (provided it was previously assigned to a valid QString*
):
解决方法分配给值,而不是指针(前提是它之前分配给了有效的QString*
):
QString x("Testing");
*(Animation::m) = x;
回答by Alok Save
What is wrong with this second method ?
第二种方法有什么问题?
It crashes because you most likely access it beyond the scope in which x
was created.
它崩溃是因为您很可能在x
创建的范围之外访问它。
Automatic variables are automatically destroyed once the control exits the scope {
}
in which they were created, So beyond the scope what you have is an pointer pointing to data that does not exist. Accessing this data causes an Undefined Behavior and a crash.
一旦控件退出{
}
创建它们的范围,自动变量就会自动销毁,因此超出范围的是一个指向不存在数据的指针。访问此数据会导致未定义行为和崩溃。
How to go about it?
怎么办?
You should dynamically allocate memory and then copy the string to the dynamically allocated pointer so that you are able to access it everywhere. This way the string remains valid unless and untill explicitly delete
ed.
您应该动态分配内存,然后将字符串复制到动态分配的指针,以便您可以在任何地方访问它。这样字符串保持有效,除非和直到显式delete
ed。
回答by Michael Burr
I'll bet that your program crashes when you use Animation::m
after x
has been destroyed (probably by going out of scope).
我敢打赌,你的程序Animation::m
在x
被销毁后使用时会崩溃(可能是因为超出范围)。
If you want to use a setter to assign to Animation::m
, you'll need to pass in the argument as a pointer or by reference:
如果要使用 setter 分配给Animation::m
,则需要将参数作为指针或通过引用传递:
class Animation
{
public:
Animation();
void set_m( QString* ptr) {
m = ptr;
}
void set_m( QString& ref) {
m = &ref;
}
private:
static QString *m;
};
However, you'll still need to make sure that whatever m
points still is still alive when you try to use m
.
但是,m
当您尝试使用m
.