Set-Variable Style Functions in C++

In summary, there is not a significant difference between the two set-variable style functions for member variables in terms of efficiency. It is not recommended to have public variables for classes that are used constantly, as it is not efficient. It is best to test both ways and check for any differences, either through timing or inspecting the generated assembly code. It is also recommended to create a toy program to test the efficiency of the functions.
  • #1
neurocomp2003
1,366
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
  • #2
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.
 
  • #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) until i actually have the sim up and running...at which time i will have already had 200+ files to deal with.
 
Last edited:
  • #4
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.
 

FAQ: Set-Variable Style Functions in C++

What are set-variable style functions in C++?

Set-variable style functions are a type of function in the C++ programming language that allows for the modification of variables within a program. These functions typically take in a variable as a parameter and then change its value based on the logic within the function.

How do set-variable style functions differ from regular functions in C++?

Set-variable style functions differ from regular functions in that they specifically target variables for modification, whereas regular functions can perform a variety of tasks and may not necessarily involve variable modification. Additionally, set-variable style functions typically return a value, while regular functions do not necessarily have to.

What are the advantages of using set-variable style functions in C++?

Set-variable style functions are advantageous because they provide a structured and organized way to modify variables within a program. This can help to improve code readability and reduce the chances of errors. Additionally, these functions can be reused throughout the program, making it more efficient.

Are there any limitations to using set-variable style functions in C++?

One limitation of set-variable style functions is that they can only modify one variable at a time. This means that if a program requires multiple variables to be modified, multiple set-variable style functions would need to be created. Additionally, if the program has a large number of variables, it may become difficult to keep track of all the set-variable style functions.

Can set-variable style functions be used for any type of variable in C++?

Yes, set-variable style functions can be used for any type of variable in C++. This includes integers, floating-point numbers, characters, strings, and more. As long as the variable is passed as a parameter to the function, it can be modified using a set-variable style function.

Similar threads

Replies
19
Views
3K
Replies
1
Views
1K
Replies
17
Views
1K
Replies
40
Views
3K
Replies
3
Views
2K
Replies
6
Views
3K
Back
Top