C/C++ Set-Variable Style Functions in C++

  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    C++ Functions
AI Thread Summary
The discussion centers on the efficiency of two different member variable access methods in C++: returning a reference versus using a setter function. The first method, returning a reference to the member variable, is noted for its potential efficiency, particularly when inlined by the compiler. The second method, using a setter, involves copying the value, which may introduce overhead. Participants emphasize the importance of testing both approaches to determine performance differences, suggesting the use of timing tests or examining generated assembly code. The conversation also touches on the design choice between using public member variables directly (e.g., vector._x) versus accessor methods (e.g., vector.x()). The consensus is that while public variables may seem simpler, accessor methods are generally preferred for encapsulation and maintainability, with efficiency concerns being secondary. Testing with smaller programs is recommended to evaluate performance impacts before implementing in larger simulations.
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
 
Technology 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:
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top