Skip to main content
GameDev.net gamedev.net
🔒 Locked

pointer constructor

Started by BloodLust666 Aug 16, 2006 at 10:27 AM 11 replies 1.2k views
Original Post
BloodLust666
BloodLust666
when creating a pointer, does it call it's constructor? myType *pObj = NULL; pObj = pDifferentObject; did pObj call it's constructor?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
SiCrane
SiCrane
If I understand what you're asking, the answer is no.
CoyCash
CoyCash
I would have to say a big 'no' to this. pObj is not instantiated. A constructor would have allocated the memory needed for myType and pointed pObj to it, but as it lies pObj begins as nothing more than a pointer to null and then points to another object's (pDifferentObject) allocated memory.
MrAccident
MrAccident
Nope, no constructor call. You have not actually created any instances of myType in the snippet above - you just created a pointer, initialized it to NULL, and then assigned to it a different pointer to what I assume is an already-existant object.
BloodLust666
BloodLust666
ok, then that's good. because I have a singleton object that has a static object to make sure there's only one and if there's more, it deletes that one in the constructor.

now in the actual program (the singleton is apart of my engine), i need a pointer to that object but i don't want to create one, i just want to point to it.

-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
ToohrVyk
ToohrVyk
Quote:
Original post by EvilKnuckles666
ok, then that's good. because I have a singleton object that has a static object to make sure there's only one and if there's more, it deletes that one in the constructor.


That's a bad way to go about it, in my opinion. At worst, you should cause an assert failure when such a thing is detected. At best, you would make the constructor private and only call it once from a member function if the object is not initialized yet.

Destroying the previous version when a new one appears is bound to create some surprising bugs if this accident ever happens.
superdeveloper
superdeveloper
Quote:
Original post by SiCrane
If I understand what you're asking, the answer is no.


Just to add to this for clarity...


myType pObj; // this will call the constructor upon declaration, not a pointer.


myType * pObj = new myType; // this will call the constructor, and is a pointer.


myType * pObj = pDifferentObject; // this will not call constructor, it's just an assignment of an existing pointer to an object (the pDifferentObject may or may not be NULL).

MrAccident
MrAccident
To expand a bit on what ToohrVyk said, the best way to implement a singleton is to hide the constructor (declare it as private), and just initialize your static instance pointer to NULL there. Then provide a static function called GetInstance that checks the static instance - if it's null, create a new one and return it; if it's not null, just return the one that already exists. Then, destroy it in the destructor. This makes it almost impossible to create more than one instance of your singleton object.
ToohrVyk
ToohrVyk
Quote:
Original post by MrAccident
Then, destroy it in the destructor.


Destroy what, in what destructor?
superdeveloper
superdeveloper
Quote:
Original post by ToohrVyk
Quote:
Original post by MrAccident
Then, destroy it in the destructor.


Destroy what, in what destructor?


Better yet, the static object can be created automatically at runtime, and when the program ends, the destructor automatically also gets called, shutting down the singleton.

Declare the object as a non-pointer static within itself. When program leaves scope, all static objects automagically are destroyed.
MrAccident
MrAccident
Quote:
Original post by ToohrVyk
Destroy what, in what destructor?

Woops! Heheh, I wasn't thinking for a second there. Obviously, I meant in a static DestroyInstance method. The keys are all right next to each other. [grin]

But superdeveloper's method is more elegant anyway.
Bregma
Bregma
Quote:
Original post by EvilKnuckles666
when creating a pointer, does it call it's constructor?

myType *pObj = NULL;
pObj = pDifferentObject;

did pObj call it's constructor?


The answer is that built-in types do not have constructors. If you're expecting the myType constructor to be called, it is not, because no object of type myType has been created. You've created an object of type pointer-to-myType, which is of built-in type pointer-to-object.

Built-in types do not have constructors, and operators on built-in types cannot be overloaded.
Stephen M. Webb
Professional Free Software Developer
Zahlman
Zahlman
Quote:
Original post by Bregma
operators on built-in types cannot be overloaded.


To be pedantic, unary operators on built-in types cannot be overloaded, nor binary operators where both arguments are built-in types; but you can certainly overload binary operators where one side is a built-in type and the other is not. (There is one ternary operator but it cannot be overloaded; and no operators of other arity.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.