My code is below. However before main() is run something simple such as a static std::string globalvar; will call new. Before MyPool mypool is initialized.
MyPool mypool;
void* operator new(size_t s) { return mypool.donew(s); }
Is there anyway I can force mypool to be initialized first? I have no idea how overloading new is suppose to work if there is no way to initialize its values so I am sure there is a solution to this.
I am using both visual studios 2010 and gcc (cross platform)
Make
mypoola static variable of youroperator newfunction:It will be initialized upon first call of the function (i.e. the operator
new).EDIT: As the commenters pointed out, declaring the variable as
staticin theoperator newfunctions limits its scope and makes it inaccessible in theoperator delete. To fix that, you should make an accessor function for your pool object:and invoke it in both
operator newandoperator delete:As before, declaring it as static local variable guarantees initialization upon first invocation of
GetMyPoolfunction. Additionally, it will be the same pool object in both operators which likely what you want.