Initialzing of Ogre Objects with raw Pointers

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
Alpatzino
Gnoblar
Posts: 2
Joined: Tue Feb 04, 2020 5:06 pm

Initialzing of Ogre Objects with raw Pointers

Post by Alpatzino »

Hey guys,

I have basic question about the initialization of ogre variables.

Things like SceneManager, nodes and Lights are always initialized with a raw pointer e.g.:

Code: Select all

Ogre::SceneManager* scnMngr = Ogre::SceneManager* scnMgr = root->createSceneManager();
Ogre::SceneNode* node = scnMngr->getRootSceneNode()->createChildSceneNode();
Why are they initialized with raw pointers and not just normal like:

Code: Select all

Ogre::SceneManager scnMngr = Ogre::SceneManager* scnMgr = root->createSceneManager();
Ogre::SceneNode node = scnMngr->getRootSceneNode()->createChildSceneNode();
Is it because of polymorphism or/and we ahve a an interface with a C-library?

This article tells me that raw pointers are one of the last options I should use, so why is it needed in this case? Or what would be the a modern way to initialize the variables?
Article: https://stackoverflow.com/questions/221 ... ect-itself

Thanks for reading and have a nice day :)

User avatar
sercero
Bronze Sponsor
Bronze Sponsor
Posts: 62
Joined: Sun Jan 18, 2015 4:20 pm
Location: Buenos Aires, Argentina
x 13

Re: Initialzing of Ogre Objects with raw Pointers

Post by sercero »

If you use raw pointers, then the object is allocated on the heap.

Otherwise the object is allocated on the stack where you have less memory available and it should be used in general for objects that do not take too much memory.

I don't know where you read that you should not use pointers, but it might be good advice or not based on context.

Perhaps they said that you should use pasage by reference instead of pointers so the syntaxis is more readable.

I am not a C++ expert so maybe someone will enlighten us further.

Here is a good explanation I found on Google:
https://gribblelab.org/CBootCamp/7_Memo ... _Heap.html

Best regards,
Guillermo

paroj
OGRE Team Member
OGRE Team Member
Posts: 1118
Joined: Sun Mar 30, 2014 2:51 pm
x 401
Contact:

Re: Initialzing of Ogre Objects with raw Pointers

Post by paroj »

as most constructs in C++, raw pointers have multiple meanings, where heap allocation is just one. Others are
- reference to data (C style). Also note that you cannot store C++ references in a list.
- non-owning see https://github.com/isocpp/CppCoreGuidel ... .md#Rr-ptr
- optional value (as in nullable)

in ogre pointers are mostly used to indicate a non-owning reference to data. Sometimes they also indicate optional value e.g. in SceneNode::getParent().

Alpatzino
Gnoblar
Posts: 2
Joined: Tue Feb 04, 2020 5:06 pm

Re: Initialzing of Ogre Objects with raw Pointers

Post by Alpatzino »

Hi guys,

thanks for your responses and the good links to explanations, I learned something new again.

@sercero the link in my question was what I was refering to.

Stay healthy :)

User avatar
sercero
Bronze Sponsor
Bronze Sponsor
Posts: 62
Joined: Sun Jan 18, 2015 4:20 pm
Location: Buenos Aires, Argentina
x 13

Re: Initialzing of Ogre Objects with raw Pointers

Post by sercero »

Hey Al Patzino, yes I realized late that there was a link.

Pretty interesting topic, thanks.

Post Reply