-1

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
coming out of void
  • 1,454
  • 2
  • 12
  • 12

3 Answers3

1

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();
sean e
  • 11,792
  • 3
  • 44
  • 56
  • 2
    Globals have static storage duration and are zero-initialized already. –  Apr 16 '10 at 06:47
1

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;
rocknroll
  • 1,128
  • 2
  • 15
  • 30
  • 1
    More complication than needed in both declaring the global and managing it. –  Apr 16 '10 at 06:04
  • actully it is not possible to pass object to functions .we are not calling fuctions just returning next event which indirectly calls the fuction – coming out of void Apr 16 '10 at 06:12
1

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.

Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75