[英]0xC0000005: access violation in C++
Here is my code: 这是我的代码:
class Base
{
public:
virtual void show() const = 0;
};
class Child : public Base
{
private:
static const int i = 1;
public:
virtual void show() const
{
cout << i;
}
};
map<int, const Base &> myMap{
{ 0, Child() },
{ 1, Child() },
};
Base & b = Child();
int main()
{
b.show();
myMap.at(0).show(); // This provokes the error
system("pause>NUL");
return 0;
}
As you see, I'm trying to use a global
(or static
) data, which will call some virtual
functions. 如您所见,我正在尝试使用global
(或static
)数据,这将调用一些virtual
函数。 When I test Base & b = Child();
当我测试Base & b = Child();
and in main
: b.show();
在main
: b.show();
, everything goes well. , 一切顺利。
But, if I use map
like above, I will get an error: 但是,如果我使用上面的map
,我将收到一个错误:
0xC0000005: Access violatoin reading location 0x00000000
.0xC0000005: Access violatoin reading location 0x00000000
。
I've tried to debug this code and I found that when it arrived myMap.at(0).show();
我试图调试这个代码,我发现当它到达myMap.at(0).show();
, I got this: , 我懂了:
It seems that the table of virtual functions is Unable to read
... 似乎虚函数表Unable to read
...
Then I tried to use the pointer: 然后我尝试使用指针:
map<int, Base *>
and {0, new Child()}
. map<int, Base *>
和{0, new Child()}
。
This works. 这有效。
So it seems that this error comes from temporary reference. 所以似乎这个错误来自临时引用。
But I don't know why b
works. 但我不知道为什么b
有效。 b
is a temporary reference too. b
也是临时参考。
In my opinion, this map
contains many b
. 在我看来,这张map
包含很多b
。
Why does b
works whereas map
doesn't work? 为什么b
工作而map
不起作用?
You have a map of references to temporaries. 你有一个临时工具的地图。 I'm surprised that even compiled 即使编译,我也很惊讶
map<int, Base &> myMap{
{ 0, Child() },
{ 1, Child() },
};
Drop the reference and switch to unique_ptr
删除引用并切换到unique_ptr
std::map<int, std::unique_ptr<Base>> myMap;
You can insert elements into this map using this method . 您可以使用此方法将元素插入此贴图中。 This will prevent object slicing . 这将阻止对象切片 。
You do the same thing again here 你在这里再做同样的事情
Base & b = Child();
You cannot hold non-const references to temporary objects 您不能保存对临时对象的非const引用
MSVC does allow to affect a temporary to a reference, meaning that Base & b = Child();
MSVC允许影响临时引用,这意味着Base & b = Child();
is accepted and correctly processed, even it is not standard C++ and is rejected by other compilers (and could be rejected by a later version of MSVC) 被接受并正确处理,即使它不是标准的C ++并被其他编译器拒绝(并且可能被更高版本的MSVC拒绝)
But even MSVC does not accept stl containers of references. 但即使是MSVC也不接受stl容器的引用。
So you should either use a map<int, Base>
(no ref, store a copy of the object), or a map<int, Base *>
(stores a pointer to the object). 所以你应该使用map<int, Base>
(没有ref,存储对象的副本),或者map<int, Base *>
(存储指向对象的指针)。 In the latter case, you must deal explicitely with the destruction of the object. 在后一种情况下, 您必须明确地处理对象的破坏。 You can also use smart pointers , unique_ptr
or shared_ptr
to let the stl cares for automatic destruction. 您还可以使用智能指针 , unique_ptr
或shared_ptr
让stl关心自动销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.