Set-Variable Style Functions in C++

  • Context: C/C++ 
  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    C++ Functions
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 5K views
neurocomp2003
Messages
1,359
Reaction score
4
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
 
Physics news on Phys.org
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.
 
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) until i actually have the sim up and running...at which time i will have already had 200+ files to deal with.
 
Last edited: