Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 MrOMGWTF   Members   -  Reputation: 440

Like
0Likes
Like

Posted 20 October 2012 - 02:18 AM

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? :
.h
extern Renderer* renderer;
.cpp
renderer = new Renderer;
Or like this:
.h
extern Renderer renderer;
.cpp
renderer = Renderer();

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

Sponsor:

#2 japro   Members   -  Reputation: 887

Like
1Likes
Like

Posted 20 October 2012 - 02:33 AM

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, 20 October 2012 - 02:37 AM.


#3 MrOMGWTF   Members   -  Reputation: 440

Like
0Likes
Like

Posted 20 October 2012 - 03:55 AM

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.


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 :)

#4 rip-off   Moderators   -  Reputation: 8764

Like
1Likes
Like

Posted 20 October 2012 - 04:51 AM

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:
// 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;
}
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.

#5 MrOMGWTF   Members   -  Reputation: 440

Like
0Likes
Like

Posted 20 October 2012 - 06:08 AM

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:

// 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;
}
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.


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, 20 October 2012 - 06:09 AM.


#6 rip-off   Moderators   -  Reputation: 8764

Like
1Likes
Like

Posted 20 October 2012 - 07:46 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS