const declaration at the end of function in C ++ and const before the argument in method

2

I came across this snippet of code in a material:

class Foo 
{
public:
int Bar(int arg1) const //<-- qual função do const aqui?
{
    // código a ser implementado aqui
}
};

[1] How does const affect this statement of method Bar ?

[2] What's the difference between these statements?

const int * ptrInt1;
int const * ptrInt2;

[3] The method with const in the argument, is so that the parameter forced to be read only in code implementation?

int Foo (const int Bar) //<-- const antes de argumento na função
{
   // código a ser implementado aqui
}
    
asked by anonymous 09.08.2015 / 14:15

1 answer

2
  • With this type modifier it makes clear to the programmer and the compiler that the object will not be modified. Members who save status will not be changed by this method. It is an important protection in many cases where you do not want to allow members to be changed, except using the keyword mutable .

    class C { //exemplo da Wikipedia
        int i;
        public:
            int Get() const {
                return i;
            }
            void Set(int j) {
                i = j;
            }
        };
    
        void Foo(C& nonConstC, const C& constC) {
            int y = nonConstC.Get(); // Ok
            int x = constC.Get();    // Ok: Get() é const
    
            nonConstC.Set(10); // Ok: nonConstC é modificável
            constC.Set(10);    // Erro! Set() permite modificar o objeto e constC é um objeto constante
       }
    

    }

  • These two forms are equivalent, there is no semantic difference, only syntactic. But there can be when const is in other places. Only the% of initial% can be inverted in its position. It is possible to use const for the pointer and it must always come later to avoid creating ambiguity.

    The position indicates who is constant, the content or the pointer:

     int * - ponteiro para int
     int const * - ponteiro para um int constante
     int * const - ponteiro constante para um int
     int const * const - ponteiro constante para um int constante
    

    You should already know that the statement in C and C ++ is the other way around.

    By constant pointer understand that you can not change the value of the variable (point to another object) but you can change the content pointed to by it, unless it has also been declared constant.

  • You're right, but in this way there's not much of an advantage. This means that the const value can not be changed within this function. It has the advantage of making it clear and preventing it from changing this value wrongly. But the effect is just local, easy to control.

    This is more important when a parameter receives values by reference or is a pointer form, after all, changing the parameter will affect the value of the argument used in the call. This is a side effect and has consequences. In general, one should avoid changing parameters that can generate as much collateral effect as possible. Of course there are several cases that it is necessary to allow the argument to change, which is precisely the desired effect.

  • The const correctness call has a number of holes and one should be very careful about its use. These are simplifications of the explanation. Of course if you want to better understand each use should look for documentation or open more specific questions on each aspect.

        
    09.08.2015 / 14:47