Connecting Tech Pros Worldwide Forums | Help | Site Map

global object and new operator

Neo
Guest
 
Posts: n/a
#1: Aug 29 '08
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?



Anonymous Infidel - Aborted Islam with a hanger
Guest
 
Posts: n/a
#2: Aug 29 '08

re: global object and new operator


Hi Friends,
Quote:
* *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.
Closed Thread