PDA

View Full Version : Set-Variable Style Functions in C++


neurocomp2003
Jan12-06, 01:42 AM
Just curious if there was any difference between the following two set-variable style functions for member variables interms of effiecieny

CType& Member(void) {return _member; } //Used in David Eberly's Code
void Member(CType const& tp) {_member=tp); }

Is there an actual difference?
Also is it better to have public variables for classes that are used constantly like a vector._x or to have them as vector.x()

Heh i should really go to coding forums for this but I've never really used a C/C++ Coding forum before. Maybe i'll go browse gamedev

Hurkyl
Jan12-06, 04:01 AM
The best way to tell is always to try it both ways and check. :smile: You can check either by setting up a test case for timing the difference, or you can look to the generated assembly code for differences.

All of these functions that you mention ought to be inlined... meaning that when you write a function call, the compiler will really replace it with the body of the function, instead of putting a real function call in the compiled code.


Also is it better to have public variables for classes that are used constantly like a vector._x or to have them as vector.x()

Most emphatically no. But I suspect you meant in terms of efficiency, and the answer is the same as above.

neurocomp2003
Jan12-06, 01:52 PM
yeah i could do that =](but i don't know how or have the time...deadlines ugh)...but i'm looking to do large scale sims..and i won't be able to tell the effect(if there is a significant one) untill i actually have the sim up and running...at which time i will have already had 200+ files to deal with.

Hurkyl
Jan12-06, 06:43 PM
You don't necessarily have to test it with the full sim -- just make a toy program that should be sufficient to put it through its paces.