Should a static object be dynamically allocated?

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace System {

class Context {
public:
	static Context* Create(){return new Context;}
	Program* GetProgram()const{return Program_Ptr;}
	Timer* GetTimer()const{return Timer_Ptr;}
	Input* GetInput()const{return Input_Ptr;}
	Render* GetRender()const{return Render_Ptr;}
	~Context(){
                std::cout << "Context dtor!" << std::endl;
		delete Program_Ptr; 
		delete Timer_Ptr;
		delete Input_Ptr;
		delete Render_Ptr;
		delete this;
	}
private:
	Program* Program_Ptr;
	Timer* Timer_Ptr;
	Input* Input_Ptr;
	Render* Render_Ptr;
	Context(){
		Program_Ptr = System::Program::Create(*this);
		Timer_Ptr = System::Timer::Create(*this);
		Input_Ptr = System::Input::Create(*this);
		Render_Ptr = System::Render::Create(*this);
	}
};

static Context* The_Context = Context::Create();
}


I don't see the context dtor std::cout ever makes it to the console window. Is it better to just make a normally allocated static object instead?
If you want to destroy a dynamically allocated object, static or not, you have to use delete.
Unless there are any reasons, you should allocate your objects statically, to prevent memory leaks and other pointer related problems. They are often hard to find..
Topic archived. No new replies allowed.