(仅当零初始化为静态范围成员时才不是垃圾。)它在 GCC 上按预期工作!(((
所以对于像这样的代码:
#include <iostream>
#include <boost/shared_ptr.hpp>
namespace SP
{
// enums
enum EnumMessage {
EnumMessage_EventBase = 0,
EnumMessage_EventServiceBase = 1,
EnumMessage_OperationBase = 2
};
class Message;
// define proxy class
class Message {
public:
EnumMessage _MessageCode;
};
class Parent : public Message {
public:
int _ServiceId;
int _CallbackId;
};
class Child : public Parent {
public:
std::string _Debug;
};
class AnotherChild : public Parent {
public:
int _UserId;
};
}
using namespace SP;
int main() {
//OK
static Child staticScopeChild = Child();
boost::shared_ptr<Parent> ptrParent(new Parent());
boost::shared_ptr<AnotherChild> ptrChild2(new AnotherChild());
//Bad
Child scopeChild = Child();
boost::shared_ptr<Child> ptrChild(new Child());
std::cout << "static " << staticScopeChild._MessageCode
<< std::endl << "vs scope " << scopeChild._MessageCode
<< std::endl << "vs pointer " << ptrChild->_MessageCode
<< std::endl << "vs parent class pointer: " << ptrParent->_MessageCode
<< std::endl << "vs another parent child: " << ptrChild2->_MessageCode <<std::endl;
std::cin.get();
return 0;
}
所有类通常都是 POD ( ints
, enums
) 我得到下一个输出:
static 0
vs scope -858993460
vs pointer -842150451
vs parent class pointer: 0
vs another parent child: 0
虽然我希望一切都会0
!
为什么会发生这样的事情?