global object and new operator

N

Neo

Hi Friends,
I want to create a single instance of my class and use it through
out my program.
Simple solution is, I can make a static method and invoke it by
different callers. So first caller will be responsible for creating
the object. To make this behavior thread safe I will be protecting the
object creation call in a critical section.

Now another idea I have is of putting dynamic allocation in global
space instead of a static method like

*point_to_object = new class_name(); // this will be in global space

I saw that this works well. I can destroy this object during the exit
of my program. I am interested in knowing that are will there be any
issues related to run time libraries, platform specific etc?
 
A

Anonymous Infidel - Aborted Islam with a hanger

Hi Friends,
   I want to create a single instance of my class and use it through
out my program.
 Simple solution is, I can make a static method and invoke it by
different callers. So first caller will be responsible for creating
the object. To make this behavior thread safe I will be protecting the
object creation call in a critical section.

  Now another idea I have is of putting dynamic allocation in global
space instead of a static   method like

 *point_to_object = new class_name();  // this will be in global space

I saw that this works well. I can destroy this object during the exit
of my program. I am interested in knowing that are will there be any
issues related to run time libraries, platform specific etc?
Well, I'm not a professional programmer but I would go global, and to
make it easy I would write a function to access the class using
extern. [Save time remembering class name]

extern Object *point_to_object;
static Object *GetObject()
{
return point_to_object;
}
As for thread safety I would just put the CS(critical section) code in
your global "point_to_object" class.

Note: I would also make your CS code a macro so if you have to change
it in the future.

Have a nice day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top