Often in the books I met the construction of the form:

void add_foo (const Foo &) const;

The book explains that it is better to use constant methods so that you can immediately see where things are changing and where not.

Should I pass an object by reference using the constant method? Or should it be better to simply transmit without reference and without const? Besides the reluctance to copy a large object into memory, I can’t think of a reason to use the “perverted” transmission by reference. In fact, we take a tool for transmission with the ability to change the original object and prohibit it. Is it worth doing this all the time? Maybe there are some more important reasons that I just do not know?

    3 answers 3

    You should always remember about two aspects of the perception of your code - the compiler and the next programmer.

    From the point of view of the compiler and the environment, keep in mind the optimization. Despite gigabytes and megahertz), what if it throws you into embedded tomorrow.

    But it is much more important to remember the readers of your code! void add_foo (const Foo & foo) clearly states that add_foo will not change the state of the foo object and will only use const methods of the Foo class. Similarly, specifying a method as const indicates the immutability of the state of class objects after the call.

    Should I pass an object by reference using the constant method?

    Whenever possible)

    Or should it be better to simply transmit without reference and without const?

    In practice, it is extremely rare to see the transfer of a copy of an object, excluding POD types. And, for example, for a class using resources (SomeFile) it is just fraught.

    In fact, we take a tool for transmission with the ability to change the original object and prohibit it.

    RTFM http://www.stroustrup.com/bs_faq2.html#pointers-and-references

    PS Why did I mention the environment? For example, in Qt lazy copying is widely used, and therefore there is little practical difference in the transfer of a copy or link. But, again, remember about the reader.

    • Thank you for such a detailed answer. Began to use everywhere the indication const when passing by reference. As a result, after a couple of tasks, VS 2013 began to issue such warnings. anachronism used: qualifiers on reference are ignored. On such a code. 'Foo nameFunction (Foo & const Foo1)' Here are the comments of MS themselves about this. [ social.msdn.microsoft.com/Forums/vstudio/en-US/… . It turns out so better not to do? Help to understand, please. - Denis Savenko
    • The answer you almost found yourself. He is the last on the given - erapid
    • Upps ... you found it yourself. Look carefully at the last answer on the link given by you. And, for consolidation, here ( isocpp.org/wiki/faq/const-correctness#const-ref-nonsense ). Since you, as it seemed to me, a searching programmer, I recommend to study the FAQ on this site closely. - erapid
    • @ denis-savenko. And, just in case, an explicit answer. const Type & ref - a reference to an immutable type object. Type & const object - a permanent reference to an object of type. These are different things, and the second does not make sense based on the nature of the links. While const Type * const pointer is quite a normal construction) - erapid
    • Thank. Now more or less clear. It will be necessary for a couple of examples to be sorted out in order to understand. Thanks for the link. - Denis Savenko

    In my opinion, you are confusing class constant methods and constant parameter references. The constant method does not change the state of an instance of its class, and not the parameters passed.

    • Yes. But when two objects are added together, without using the overloaded + in the book they write, they say it’s better to make the constant and reference parameter, I understand that in the method itself not to change another object. I wanted to understand why, and not to give a copy. - Denis Savenko

    Yes you are right.

    Transfer via a constant link is practically (with proper programming) no different from transferring a copy. The reason is usually to optimize unnecessary copying.


    Okay, there is a case where there really is a difference. For example, if you want to save a reference to an object and monitor it by calling getter methods. Moreover, if the object is transmitted by const-link, you will actually see the changes of the transferred object. If you are given a copy, you will only see changes to the copy. As we know, const means only a read-only view on an object, but does not mean that the object itself cannot change.


    However, in most cases the same thing is meant, and the transfer is done by const-link only for optimization.


    A const (“in order not to change it accidentally”) can be used also in transmission by value, yes.

    • Thank you, explanation about tracking the change of the object itself seems to me a sufficiently weighty reason to write like that) I will do that then ... - Denis Savenko