Page 1 of 1

Initialzing of Ogre Objects with raw Pointers

Posted: Wed Mar 11, 2020 5:06 pm
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 :)

Re: Initialzing of Ogre Objects with raw Pointers

Posted: Wed Mar 11, 2020 11:25 pm
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

Re: Initialzing of Ogre Objects with raw Pointers

Posted: Thu Mar 12, 2020 12:27 pm
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().

Re: Initialzing of Ogre Objects with raw Pointers

Posted: Thu Mar 19, 2020 6:32 pm
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 :)

Re: Initialzing of Ogre Objects with raw Pointers

Posted: Fri Mar 20, 2020 1:57 am
by sercero
Hey Al Patzino, yes I realized late that there was a link.

Pretty interesting topic, thanks.