Class constructor parameter question...
-
- Kobold
- Posts: 31
- Joined: Mon Dec 30, 2002 6:27 pm
- Location: Memphis TN.
Class constructor parameter question...
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*
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*
- CaseyB
- OGRE Contributor
- Posts: 1335
- Joined: Sun Nov 20, 2005 2:42 pm
- Location: Columbus, Ohio
- x 3
- Contact:
Re: Class constructor parameter question...
These two are the sameDersucher wrote:Weather::Weather(Ogre::SceneManager *sm)
Weather::Weather(Ogre::SceneManager* sm)
and these two are the same.Dersucher wrote:Weather::Weather(Ogre::SceneManager &sm)
Weather::Weather(Ogre::SceneManager& sm)
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();
}
Code: Select all
Weather& getWeather()
{
Weather myWeather;
return myWeather;
}
- CaseyB
- OGRE Contributor
- Posts: 1335
- Joined: Sun Nov 20, 2005 2:42 pm
- Location: Columbus, Ohio
- x 3
- Contact:
-
- Kobold
- Posts: 31
- Joined: Mon Dec 30, 2002 6:27 pm
- Location: Memphis TN.
- xavier
- OGRE Retired Moderator
- Posts: 9481
- Joined: Fri Feb 18, 2005 2:03 am
- Location: Dublin, CA, US
- x 22
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,
I find much more readable than
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;
};
Code: Select all
class MyClass
{
private:
int m_iVar;
MyType *m_pType;
MyOtherType *m_pOtherType;
int m_iVar2;
};
- Kojack
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 534
Whitespace has virtually no meaning to C++. Apart from things like nested template parameters.
All the following are identical:
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.
That looks like you have 2 int pointers, when really the * only applies to "a".
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.
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;
Code: Select all
int *a, b;
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.
- betajaen
- OGRE Moderator
- Posts: 3447
- Joined: Mon Jul 18, 2005 4:15 pm
- Location: Wales, UK
- x 58
- Contact:
- haffax
- OGRE Retired Moderator
- Posts: 4823
- Joined: Fri Jun 18, 2004 1:40 pm
- Location: Berlin, Germany
- x 7
- Contact:
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..
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..
-
- Goblin
- Posts: 214
- Joined: Tue Nov 21, 2006 11:11 pm
- betajaen
- OGRE Moderator
- Posts: 3447
- Joined: Mon Jul 18, 2005 4:15 pm
- Location: Wales, UK
- x 58
- Contact:
I've gotten in to the habit now of declaring arguments in classes as:
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
Code: Select all
class foo {
void setDensity(float density, Body*);
};
When NxOgre 0.9 is released you'll see this weird system all over the place
-
- Goblin
- Posts: 214
- Joined: Tue Nov 21, 2006 11:11 pm
-
- Greenskin
- Posts: 102
- Joined: Mon Jul 31, 2006 7:56 pm
- Location: Singapore
- Contact:
-
- Goblin
- Posts: 282
- Joined: Sat May 14, 2005 9:20 pm
- x 1
But what about if you want to declare more than one pointer on the same line?
looks cleaner than
Code: Select all
int *x, *y, *z;
Code: Select all
int* x, *y, *z;
- haffax
- OGRE Retired Moderator
- Posts: 4823
- Joined: Fri Jun 18, 2004 1:40 pm
- Location: Berlin, Germany
- x 7
- Contact: