Is it good practice to make getters and setters inline?

public: inline int GetValue() const { return m_nValue; } inline void SetValue(int nNewValue) { this -> m_nValue = nNewValue; } 

In Learn C ++, they said that it would work faster. So, I thought it would be great to use on getters and setters. But maybe there are some disadvantages?

+62
c ++ performance getter-setter inline
Jun 10 '10 at 18:21
source share
11 answers

I don’t embed anything until the profiler told me specifically that not embedding leads to performance problems.

The C ++ compiler is very smart and will almost certainly automatically turn on such a simple function as this is for you. And, as a rule, it is smarter than you, and it will be much better to determine what should or should not be embedded.

I would not think about what to do or not, and focus on the solution. Adding the inline later (which is not a guarantee of built-in BTW) is very easy to do, and potential locations can be easily found using the profiler.

+47
Jun 10 '10 at 18:25
source share

If you write them inside a definition, they are considered inline by default .

This means that they will be allowed in several compilation units (since the class definitions themselves usually appear in several compilation units), and not that they will actually be inlined.

+27
Jun 10 '10 at 18:26
source share

This is bad practice in the public API. Any change to these features requires recompilation of all clients.

In general , in which receivers and setters exhibit poor abstraction, do not do this. If you constantly switch to the source data in another class, you will most likely need to organize your classes, instead consider how you want to manipulate the data in the class and provide the appropriate methods for this.

+16
Jun 10 '10 at 18:27
source share

Negative points:

  • The compiler may ignore you.

  • Any change to these features requires recompilation of all clients.

  • A good compiler will embed non-built-in functions anyway when appropriate.

+10
Jun 10 '10 at 18:25
source share

I would also like to add that if you do not execute millions of queries / sets per frame, it hardly matters whether they are nested or not. Honestly, do not lose sleep.

Also, keep in mind that just because you put the word "inline" in front of your + declaration does not mean that the compiler will embed your code. He uses various heuristics to figure out if this makes sense, which is often a classic compromise of speed and size. However, there is a brute-force keyword "__forceinline" imprisoned in VC ++ (I'm not sure what it is in GCC), which stomps in heuristic compilers. I really do not recommend it at all, and besides, if you switch to a different architecture, this is likely to be wrong.

Try to put all the function definitions in the implementation file and leave clean declarations for the headers (unless, of course, you metaprogram the template (STL / BOOST / etc), and in this case almost everything is in the headers;))

One of the classic places that people like to embed (at least in video games, where I come from) is in the headings of math. Cross / point products, vector lengths, matrix cleaning, etc. Often placed in the header, which, it seems to me, is not needed. 9/10, it does not matter for performance, and if you ever need to perform a hard loop, for example, converting a large vector array to some matrix, you are probably better off manually executing the built-in math, or even better coding it platform assembler.

Oh, and one more thing, if you feel that you really need a class to be more data than code, consider using a good old structure that does not carry OO baggage with the abstraction that it is there: )

Sorry, this does not mean that there is so much, but I just think it helps to consider use cases in the real world and not depend too much on the settings of the pedantic compiler (believe me, I was there;))

Good luck.

Shane

+5
Jun 10 '10 at 23:43
source share

The code will compile a little longer and you will lose encapsulation. It all depends on the size of the project and its nature. In most cases, this is normal if they do not have complex logic.

By the way, you can skip inline if you implement directly in the class definition.

+2
Jun 10 '10 at 18:24
source share

By putting the code in the header, you are exposing your inner workings. Clients can see this and make assumptions about how your class works. This can make it difficult to change your class later without breaking client code.

+2
Jun 10 '10 at 18:30
source share

I would say that you do not need to worry about this. Read the embed FAQ .

+1
Jun 10 2018-10-10
source share

No need, start trusting compilers, at least for such optimizations!
"But not always."

+1
Jun 10 '10 at 20:23
source share

The inline keyword doesn't make sense in your case.

The compiler will embed your function if it can and wants, regardless of the keyword.

The inline keyword affects binding, not inline. It's a bit confusing, but read on it.

If the definition is in a different compilation module (the source file after the preprocessor, mainly) than the call, attachment will be possible only if optimization of the entire project and generation of the link time code are enabled. Enabling this will significantly increase the binding time (since it will practically recompile everything in the linker), but it can obviously improve performance. Not sure if it is turned on or off by default in GCC and VS.

+1
Dec 01 '15 at 14:11
source share

I must say that I do not have a strong aversion to this practice, which others seem to have in this topic. I agree that the performance gain from inlining is negligible in all but most commonly used cases. (And yes, I have come across such cases in practice.) Where I make such an insert, I do this for convenience and generally for single-line ones. In most of my use cases, the need to avoid client-side recompilation, if I ever change them, is not so strong.

Yes, you can opt out of inline , as implied when placing an implementation.

In addition, I am a little surprised that I am against accessors. You can hardly sneeze in a class in any OO language without blowing out a few words, and they, after all, are the correct technique for abstracting the implementation from the interface, so it is a little masochistic to claim them as bad OO practice. Good advice is not to write applicants indiscriminately, but I also advise you not to get carried away with jealousy in order to eradicate them.

Take this, purists. :-)

0
Jun 11 '10 at 2:04
source share



All Articles