Advertisement

bizarre - instance of class non-dynamic?

Started by January 21, 2001 07:34 PM
12 comments, last by paulcoz 24 years ago
  fruit* frt = new fruit;if (frt == NULL){   // something went wrong   // maybe not enough memory which would be weird   // because this is very small}  


and use classes instead of just structs, and then rely on the constructor like someone else said


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
We are creating a Multi-player space strategy/shoot-em-up/RPG game.
Development is well under way and we do have a playable demo.
Always looking for help.
Digital Euphoria Soft

checking for NULL may or may not work .. it all depends on how c++ standards compliant your compiler is..
see, according to the c++ standard (which if you want you can buy in .pdf form for $18 from www.ansi.org) new is supposed to throw a bad_alloc expection if the call to allocate memory fails..
so, if your compiler is compliant (sounds funny don''t it? ) then your program will just shut down..

--vat
"live highlaner, grow stronger, fight another day"--methos

machinshin@onebox.com
Advertisement
Umm, don´t know if this helps, but if you put something like

fruit *frt = new fruit;

into a HEADER file, every C(++)-File including that header will get its completely own instance of the pointer *frt (and the object allocated by new), so if you write

frt->apple = 5.0;

in one C-File, the objects seen by the other C-Files are unchanged (and probably filled with junk).

Instead, you have to declare the pointer in the header file with

extern fruit *frt;

and define it in only one C-file with

fruit *frt = new fruit;

Then all the C-Files including your header will use the same pointer and object.
I''ve decided to declare the class using non-dynamic methods for now, just because that works. I''ll try to fix this problem later (I''m definitely going to use contructors/deconstructors!). Sorry I don''t have an answer to this problem - it''s definitely something to do with dynamic memory allocation though.

My last post to this thread (the one where "I''m getting frustrated!") turned out to be a mistake in my code, so I''m maybe not quite so anxious now - I was beginning to lose faith in the compiler.

Paulcoz.

This topic is closed to new replies.

Advertisement