Please explain why const at the beginning and const at the end, and what is the difference in general?

const Cash Product::GetPrice() const { return price; } 
  • four
    Ahead, when returning by value, I don’t see any sense, so if const Cash& , then it's different ... And the second const says that the function does not change anything in the object for which it is called, so that it can be called for object. - Harry
  • In the static version, it looks like: static Cash const Product::GetPrice ( Product const & me ) { return me.price;} - AlexGlebe

2 answers 2

Using const at the beginning of a method declaration binds it to the type of the return value, i.e. says that you are returning a constant object from a method.

When you use const at the end of a method declaration, the method itself becomes constant (not changing anything in the object) and, as a result, is available for calling through constant objects.

    This announcement

     const Cash Product::GetPrice() const { return price; } 

    defines a non-static member function called GetPrice for the Product class.

    The first const qualifier refers to the return value of the function, that is, a Cash object.

    The const qualifier, after the parameter list, refers to the function itself and says that the function does not change the object of type Product for which it is called.

    For example, if an object of type Product declared as a constant object, then it is not possible to call non-static member function class members that are declared without the const qualifier.

    Also, for non-static member functions of the class, you can specify reference qualifiers that determine for which type of reference (lvalur or rvalue) the corresponding non-static member function of the class can be called on the class object.

    Below is a demo program.

     #include <iostream> struct Cash { Cash( unsigned int value ) : value( value ) { } unsigned int value; }; struct Product { Product( unsigned int price ) : price( price ) { } const Cash GetPrice() & { std::cout << " - const Cash Product::GetPrice() & is called - "; return price + 10; } const Cash GetPrice() const & { std::cout << " - const Cash Product::GetPrice() const & is called - "; return price; } const Cash GetPrice() const && { std::cout << " - const Cash Product::GetPrice() const && is called - "; return price; } unsigned int price; }; int main() { const Product p1( 10 ); std::cout << "cash" << p1.GetPrice().value << '\n'; Product p2( 10 ); std::cout << "cash" << p2.GetPrice().value << '\n'; std::cout << "cash" << Product( 10 ).GetPrice().value << '\n'; } 

    The output of the program to the console:

     cash - const Cash Product::GetPrice() const & is called - 10 cash - const Cash Product::GetPrice() & is called - 20 cash - const Cash Product::GetPrice() const && is called - 10 

    Since the object p1 is constant, it calls for a function with qualifiers const & .

    The p2 object is not constant, and a function with the qualifier of the & reference is called for it.

    The last function call uses a temporary object, so a function with const && qualifiers is called for it.