Sign in to follow this  
MrOMGWTF

Creating instance of the class, as a pointer or not?

Recommended Posts

MrOMGWTF    444
Hey, I wasn't coding in C++ for A LOT of time, I was coding in C#.
Today I decided to switch back to C++.

I don't know what to do:
There is a global variable, renderer. It's declared as extern in a header file, so I can use it from other files.
And now, should I declare it like this? :
[code]
.h
extern Renderer* renderer;
.cpp
renderer = new Renderer;
[/code]
Or like this:
[code]
.h
extern Renderer renderer;
.cpp
renderer = Renderer();
[/code]

Maybe it's a stupid question but it's a dilemma for me :s
Which option is better?

Share this post


Link to post
Share on other sites
japro    887
In the pointer case you should initialize the pointer from your main (or a function called from there) and also delete it at the end of the program/deinitialization. The main advantage of the pointer solution is imho that you can control the lifetime of the object and obviously you can also use polymorphism (say if you have RendererOpenGL and RendererD3D or so). Both approaches are valid depending on what you want exactly.
Also if you are not using a pointer you don't actually need the "= Renderer()". Just "Renderer renderer();" is ok. Edited by japro

Share this post


Link to post
Share on other sites
MrOMGWTF    444
[quote name='japro' timestamp='1350722026' post='4992079']
In the pointer case you should initialize the pointer from your main (or a function called from there) and also delete it at the end of the program/deinitialization. The main advantage of the pointer solution is imho that you can control the lifetime of the object and obviously you can also use polymorphism (say if you have RendererOpenGL and RendererD3D or so). Both approaches are valid depending on what you want exactly.
Also if you are not using a pointer you don't actually need the "= Renderer()". Just "Renderer renderer();" is ok.
[/quote]

I thought I can use polymorphism no matter if it's pointer or not? Anyway, thanks for the help. +1.
Now, let's get back to coding :)

Share this post


Link to post
Share on other sites
rip-off    10976
A value type will never act polymorphically, its type is set in stone at compile time. Only through using a pointer or reference can a given piece of code be passed different types at runtime.

You can expose the value as a reference too, which is slightly safer:
[code]
// Renderer.h

Renderer &getGlobalRenderer();

// Main.cpp

namespace {
Renderer *globalRenderer = 0;
}

Renderer &getGlobalRenderer() {
assert(globalRenderer);
return *globalRenderer;
}

int main() {
Renderer renderer;
globalRenderer = &renderer;

// Rest of program

globalRenderer = 0;
}
[/code]
This mechanism allows you to hide the dangerous raw pointer in main(), but yet still expose the global value. Note that this approach avoids dynamically allocating the renderer - but doesn't prevent you from doing so either. This also simplifies cleanup - the renderer will naturally be destroyed at the end of main(), or if you return early, or if an exception is thrown.

Share this post


Link to post
Share on other sites
MrOMGWTF    444
[quote name='rip-off' timestamp='1350730305' post='4992105']
A value type will never act polymorphically, its type is set in stone at compile time. Only through using a pointer or reference can a given piece of code be passed different types at runtime.

You can expose the value as a reference too, which is slightly safer:
[code]
// Renderer.h

Renderer &getGlobalRenderer();

// Main.cpp

namespace {
Renderer *globalRenderer = 0;
}

Renderer &getGlobalRenderer() {
assert(globalRenderer);
return *globalRenderer;
}

int main() {
Renderer renderer;
globalRenderer = &renderer;

// Rest of program

globalRenderer = 0;
}
[/code]
This mechanism allows you to hide the dangerous raw pointer in main(), but yet still expose the global value. Note that this approach avoids dynamically allocating the renderer - but doesn't prevent you from doing so either. This also simplifies cleanup - the renderer will naturally be destroyed at the end of main(), or if you return early, or if an exception is thrown.
[/quote]

Damn, I'll use it in my code, it's a nice way for allocating important global variables :D
I should re-read my C++ book...
Thanks for the help Edited by MrOMGWTF

Share this post


Link to post
Share on other sites
rip-off    10976
My preferred alternative is to not use global variables at all. With some design experience, you'll find that passing a pointer/reference to the renderer to the necessary subsystems/modules of the program is actually not that hard at all. A design that says that any point in the code can attempt to render something is a design which almost encourages unmaintainable spaghetti.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this