声明类的全局对象后系统崩溃
我对 C++ 很陌生。我在执行以下操作时遇到系统崩溃(不是编译错误):
我正在声明类的全局指针。
BGiftConfigFile *bgiftConfig;
class BGiftConfigFile : public EftBarclaysGiftConfig { }
在本课程中,我正在从 XML 文件中读取标签。当使用此指针检索值时,系统会崩溃。我正在为 verifone 终端编码。
int referenceSetting = bgiftConfig->getreferencesetting(); //system error
getreferencesetting() 是类 EftBarclaysGiftConfig 的成员函数,
我对这种情况下指针的行为感到困惑。我知道我做错了,但无法纠正。
当我在本地声明一个类对象时,它会正确检索该值。
BGiftConfigFile bgiftConfig1;
int referenceSetting = bgiftConfig1.getreferencesetting(); //working
但如果我将这个对象声明为全局的,它也会使系统崩溃。
我需要在代码中的不同位置获取值,因此我被迫使用全局的东西。
如何纠正这个问题?
I am very new to c++. I am getting system crash (not compilation error) in doing following:
I am declaring global pointer of class.
BGiftConfigFile *bgiftConfig;
class BGiftConfigFile : public EftBarclaysGiftConfig { }
in this class I am reading tags from XML file. it is crashing system when this pointer is used to retrieve value. I am doing coding for verifone terminal.
int referenceSetting = bgiftConfig->getreferencesetting(); //system error
getreferencesetting() is member function of class EftBarclaysGiftConfig
I am confused about behavior of pointer in this case. I know I am doing something wrong but couldn't rectify it.
When I declare one object of class locally it retrieves the value properly.
BGiftConfigFile bgiftConfig1;
int referenceSetting = bgiftConfig1.getreferencesetting(); //working
But if I declare this object global it also crashes the system.
I need to fetch values at different location in my code so I forced to use something global.
How to rectify this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先向前声明 BGIftConfigFile 类,然后声明指向该类对象的指针,如下所示
然后使用 new 运算符为指针对象分配空间
完成指针删除后,使用 delete 运算符将其分配
Firstly forward declare the class BGiftConfigFile and then declare your pointer to object of the class as follows
Then allocate space for your pointer object using new operator
After you are done with your pointer delete it appropriated using delete operator
您的本地是一个堆栈分配的实例。
您的全局是一个指针,需要在开始使用之前通过调用 new 进行分配:
Your local is a stack allocated instance.
Your global is a pointer and needs to be allocated via a call to new before you start using it:
不,你不需要全局的东西。您可以将此对象的非全局实例传递给需要它的代码。
No, you don't need something global. You can pass your non-global instance of this object to the code that needs it.