声明类的全局对象后系统崩溃

发布于 2024-08-29 06:31:45 字数 717 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2024-09-05 06:31:45

首先向前声明 BGIftConfigFile 类,然后声明指向该类对象的指针,如下所示

class BGiftConfigFile
 BGiftConfigFile  *bgiftConfig;
class BGiftConfigFile : public EftBarclaysGiftConfig { };

然后使用 new 运算符为指针对象分配空间

bgiftConfig = new BGiftConfigFile(); // depends upon what constructors you have defined in your class

完成指针删除后,使用 delete 运算符将其分配

delete bgiftConfig;

Firstly forward declare the class BGiftConfigFile and then declare your pointer to object of the class as follows

class BGiftConfigFile
 BGiftConfigFile  *bgiftConfig;
class BGiftConfigFile : public EftBarclaysGiftConfig { };

Then allocate space for your pointer object using new operator

bgiftConfig = new BGiftConfigFile(); // depends upon what constructors you have defined in your class

After you are done with your pointer delete it appropriated using delete operator

delete bgiftConfig;
伴我老 2024-09-05 06:31:45

您的本地是一个堆栈分配的实例。

您的全局是一个指针,需要在开始使用之前通过调用 new 进行分配:

bgiftConfig = new BGiftConfigFile();

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:

bgiftConfig = new BGiftConfigFile();
空城之時有危險 2024-09-05 06:31:45

我需要在代码中的不同位置获取值,因此我被迫使用全局的东西。

不,你不需要全局的东西。您可以将此对象的非全局实例传递给需要它的代码。

i need to fetch values at different location in my code so i forced to use someting global.

No, you don't need something global. You can pass your non-global instance of this object to the code that needs it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文