Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Set-Variable Style Functions in C++

  1. Jan 12, 2006 #1
    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
  2. jcsd
  3. Jan 12, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.

    Most emphatically no. But I suspect you meant in terms of efficiency, and the answer is the same as above.
  4. Jan 12, 2006 #3
    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.
    Last edited: Jan 12, 2006
  5. Jan 12, 2006 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Set-Variable Style Functions in C++
  1. C++ function (Replies: 3)

  2. C++ function variables (Replies: 12)

  3. Functions in c (Replies: 1)

  4. C# with C++ functions (Replies: 2)