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

gcc passing by reference

Started by daniel_i_l Mar 22, 2009 at 8:02 AM 8 replies 1.6k views
Original Post
daniel_i_l
daniel_i_l
A few years ago I wrote a rigid body simulator. Now I'm trying to port it to linux (gcc) but I'm having some trouble with references. I know found out that VC++ lets you pass temporary values by reference and gcc doesn't (according to the standard). So I have many places in my code were I do things like this:

//stripped down class
class Simulation{
	vector<RigidBody> Bodies;
	vector<unsigned int> BodyIndexs;
	void AddBody(RigidBody &Body){Bodies.push_back(Body); 
                     BodyIndexs.push_back(Bodies.size()-1);}
	vector<RigidBody>     GetBodies(){return Bodies;}
};
//then somewere in the program (Simulation is an object of type "Simulation")
Simulation.AddBody(RigidBody(...));


What's the best way to port this? do I have to change the function so that it accepts pointers instead of references and then do something like: RigidBody *b = new RigidBody(...); Simulation.AddBody(b); Or is there an easier way? I'd be great if I didn't have to go through the entire code and change all references to pointers. Thanks. [/source]
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
SiCrane
SiCrane
In this case, you just need to change your reference to a const reference.
Antheus
Antheus
Quote:
Original post by daniel_i_l
A few years ago I wrote a rigid body simulator. Now I'm trying to port it to linux (gcc) but I'm having some trouble with references. I know found out that VC++ lets you pass temporary values by reference and gcc doesn't (according to the standard).


Unless you rely on windows.h or related headers, a good start in VC is /W4 (maximum warnings) and /Za (disable language extensions).

That brings VC as close to standard as reasonable.

NotAYakk
NotAYakk
Quote:
Original post by SiCrane
In this case, you just need to change your reference to a const reference.


And make sure that Bodies.size() is a const member function.
SiCrane
SiCrane
Quote:
Original post by NotAYakk
And make sure that Bodies.size() is a const member function.

Bodies is a vector. vector<>::size() is a const member function. Edit: though I don't see what it would need to be a const member function since what needs to be const is the Body reference, not the AddBody() function.
daniel_i_l
daniel_i_l
Quote:
Original post by SiCrane
In this case, you just need to change your reference to a const reference.

Will that work even if I want to eventually change members of the RigidBody object that's being passed? Does the 'const' operator make the variable constant only in the scope of the function where it's used or will the RigidBodies in the Rigidody vector stay constant for the entire simlation?
Thanks.

"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
SiCrane
SiCrane
The RigidBody object inside the vector is a copy of the object that you pass to your function; it's not the same object. You can change it to your heart's delight.
daniel_i_l
daniel_i_l
Thank's for the help.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
NotAYakk
NotAYakk
Quote:
Original post by SiCrane
Quote:
Original post by NotAYakk
And make sure that Bodies.size() is a const member function.

Bodies is a vector. vector<>::size() is a const member function. Edit: though I don't see what it would need to be a const member function since what needs to be const is the Body reference, not the AddBody() function.


Ya, I misread the BodyIndexes.push_back to being a call to the Body passed in. Mea culpa.
drakostar
drakostar
Quote:
Original post by SiCrane
The RigidBody object inside the vector is a copy of the object that you pass to your function; it's not the same object.

Exactly. Think about what's going on here, in the original Visual C++ version. You're instantiating a local variable, passing its reference to a function, then letting it copy the entire object into the vector. The original object is pointlessly created then destroyed once it leaves scope. (This might be optimized out, but I wouldn't count on it, and you probably want a usable Debug build too.)

On the other hand, AddBody(new RigidBody(...)) should be just fine. Take the raw pointer, push it into a vector of shared_ptr<>, and you don't have to worry about deleting them.

Topic Locked

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

Sign in to reply to this topic.