How to implement Global Pointers ?
-
I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:
-
I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:
There is no class here, only a namespace. You just said they were globals, didn't you ? You can call new in the .cpp file. You could also assign to NULL, and have your classes call new if NULL. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
-
I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:
-
There is no class here, only a namespace. You just said they were globals, didn't you ? You can call new in the .cpp file. You could also assign to NULL, and have your classes call new if NULL. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
-
I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:
Declare a class in global.cpp and make a single instance of that class
class CGlobal
{
private:
CGlobal()
{
myptr = new int[32];
}~CGlobal() { delete \[\] myptr; }
};
static CGlobal globalInit;
oRion wrote: If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? Yes. Just don't derive from the class. That can be prevented by making the constructor private. The other option is to make a GlobalInit() and GlobalCleanup() function which does your allocations and call it at the beginning of your program. Todd Smith
-
Declare a class in global.cpp and make a single instance of that class
class CGlobal
{
private:
CGlobal()
{
myptr = new int[32];
}~CGlobal() { delete \[\] myptr; }
};
static CGlobal globalInit;
oRion wrote: If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? Yes. Just don't derive from the class. That can be prevented by making the constructor private. The other option is to make a GlobalInit() and GlobalCleanup() function which does your allocations and call it at the beginning of your program. Todd Smith
Todd Smith wrote: Declare a class in global.cpp and make a single instance of that class class CGlobal{private: CGlobal() { myptr = new int[32]; } ~CGlobal() { delete [] myptr; }};static CGlobal globalInit; Thanks for the reply. I understand the portion on using private for constructor. Could u help tell me more of the statement on static CGlobal globalInit; ? Where should this be declared in? Why is there a need to declare it as a static ? :confused: