Class constructor parameter question...

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
Dersucher
Kobold
Posts: 31
Joined: Mon Dec 30, 2002 6:27 pm
Location: Memphis TN.

Class constructor parameter question...

Post by Dersucher »

I feel like I should prolly know this.... And at one time I did...

But whats the difference between the various ways i've seen of passing parameters? e.g.

Weather::Weather(Ogre::SceneManager *sm)
Weather::Weather(Ogre::SceneManager* sm)
Weather::Weather(Ogre::SceneManager &sm)
Weather::Weather(Ogre::SceneManager& sm)

??

I apologize if this is an extreemly noobish question.. But for the life of me, i can't remember..

*curses VB.net*
User avatar
CaseyB
OGRE Contributor
OGRE Contributor
Posts: 1335
Joined: Sun Nov 20, 2005 2:42 pm
Location: Columbus, Ohio
x 3
Contact:

Re: Class constructor parameter question...

Post by CaseyB »

Dersucher wrote:Weather::Weather(Ogre::SceneManager *sm)
Weather::Weather(Ogre::SceneManager* sm)
These two are the same
Dersucher wrote:Weather::Weather(Ogre::SceneManager &sm)
Weather::Weather(Ogre::SceneManager& sm)
and these two are the same.

They both hold the address in memory of the object that they are referencing. The difference is that an object that is "new"ed to get a pointer is created on the heap and will not go out of scope, an object that is just defined and you make a reference to is created on the local stack, so it can go out of scope. For example if you do something like this:

Code: Select all

Weather* getWeather()
{
    return new Weather();
}
then the Weather object will be created on the heap and will be valid when the pointer is used by the calling function. If you were to do this:

Code: Select all

Weather& getWeather()
{
    Weather myWeather;
    return myWeather;
}
Then myWeather will be created on the local stack and will go out of scope when the method returns and the reference passed to the calling function will be invalid.
Image
Image
Dersucher
Kobold
Posts: 31
Joined: Mon Dec 30, 2002 6:27 pm
Location: Memphis TN.

Post by Dersucher »

Ahhh.. Yes... Horrid memories are returning.. Heap stack, local stack.. ;-)

Thank you for your reply.

So using Ogre::SceneManager* sm or Ogre::SceneManager *sm either one has no difference?
User avatar
CaseyB
OGRE Contributor
OGRE Contributor
Posts: 1335
Joined: Sun Nov 20, 2005 2:42 pm
Location: Columbus, Ohio
x 3
Contact:

Post by CaseyB »

Dersucher wrote:So using Ogre::SceneManager* sm or Ogre::SceneManager *sm either one has no difference?
Nope, it's just a matter of preference.
Image
Image
Dersucher
Kobold
Posts: 31
Joined: Mon Dec 30, 2002 6:27 pm
Location: Memphis TN.

Post by Dersucher »

Cool.. ;-) Many thanks..

One last question, then I will answer you these questions three. :-)

Given what was previously stated, when you "New" something, does the same hold true?

Weather *wxSystem;
wxSystem = new Weather(mSceneMgr);

vs.

Weather* wxSystem;
wxSystem = new Weather(mSceneMgr);
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Post by xavier »

Yes, the difference is only preference, as stated. Where the * shows up (what it's "attached" to) makes no difference to the compiler.

My personal preference has become the "Type* pVariable" style -- since the type of the identifier is a "Type*" it's easier to scan code and see what "pVariable" actually is. Much easier in class declarations where large spaces are employed between the identifier type and its name...for instance,

Code: Select all

class MyClass
{
private:
     int                    m_iVar;
     MyType*                m_pType;
     MyOtherType*           m_pOtherType;
     int                    m_iVar2;
};
I find much more readable than

Code: Select all

class MyClass
{
private:
     int                   m_iVar;
     MyType                *m_pType;
     MyOtherType           *m_pOtherType;
     int                   m_iVar2;
};
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Post by Kojack »

Whitespace has virtually no meaning to C++. Apart from things like nested template parameters.
All the following are identical:

Code: Select all

Weather *wxSystem;

Weather* wxSystem;

Weather*wxSystem;

Weather * wxSystem;

Weather             *            wxSystem;

Weather 
      *
wxSystem;

I prefer the * with a space on the left, because it more closely matches the way c++ actually works. The * binds to the right when defining variables, so I put it closer to the thing on the right.

Code: Select all

int* a, b;
That looks like you have 2 int pointers, when really the * only applies to "a".

Code: Select all

int *a, b;
That shows how the * groups with variables better.

But the compiler doesn't care, and I don't like mixing pointers and non pointers in one line anyway. So it all comes down to personal preference.
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58
Contact:

Post by betajaen »

I prefer using the system where Weather* mxSystem indicates this class creates it, and Weather *mxSystem this class just uses it.

No different at all to the compiler, but it's very handy to it look very quickly.
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 7
Contact:

Post by haffax »

betajaen, nifty trick to squeeze extra info in the source, but a bit non-standard, never heard of that one before, and the difference were lost on me. ;)

I too like Type* var better than Type *var. While the latter is syntactically more to how C++ works (though this is a C legacy really), the former expresses the semantics better. var is of type Type*, instead of "if you dereference var, then the type is Type". True to, but weird..
team-pantheon programmer
creators of Rastullahs Lockenpracht
Protagonist
Goblin
Posts: 214
Joined: Tue Nov 21, 2006 11:11 pm

Post by Protagonist »

I've seen the way betajaen places the * a fair bit I think.
I naturally fell into the habit of using Type *var in my parameter lists, and Type* var when declaring data members etc.
Makes sense to me.
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58
Contact:

Post by betajaen »

I've gotten in to the habit now of declaring arguments in classes as:

Code: Select all

class foo {
    void setDensity(float density, Body*);
};
I feel if I write "Body* body" I'm repeating myself, and Body* is just as semantic enough as Body* body. The float density is required because "float" isn't a description of what the argument is. But if the second argument wasn't there though, it would be.

When NxOgre 0.9 is released you'll see this weird system all over the place ;)
Protagonist
Goblin
Posts: 214
Joined: Tue Nov 21, 2006 11:11 pm

Post by Protagonist »

Are you doing some funky things with what is meant to happen to your objects in their future or do you mean destination? ;).

Fair point though. I tend to repeat myself doing things like Hud *hud out of habit.

EDIT: Oops, misread density as destiny, haha. Oh dear, sorry.
lXciD
Greenskin
Posts: 102
Joined: Mon Jul 31, 2006 7:56 pm
Location: Singapore
Contact:

Post by lXciD »

I'm a Type* var guy too. I like to keep things as consistent as possible. The reason why I choose that is because I was taught that way from start... ;)
Ajare
Goblin
Posts: 282
Joined: Sat May 14, 2005 9:20 pm
x 1

Post by Ajare »

But what about if you want to declare more than one pointer on the same line?

Code: Select all

int *x, *y, *z;
looks cleaner than

Code: Select all

int* x, *y, *z;
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 7
Contact:

Post by haffax »

Happens so rarely, that it is not worth worrying about.
team-pantheon programmer
creators of Rastullahs Lockenpracht
Post Reply