C ++ function syntax / prototype - data type after parentheses

I am very familiar with the declarations of the standard C / C ++ function. I recently saw something like this:

int myfunction(char parameter) const 

The above example is only hypothetical, and I don’t even know if it makes sense. I mean the part AFTER the parameter. Comp. What is it?

More real example:

 wxGridCellCoordsArray GetSelectedCells() const 

You can find it here. So what is const text at the end of a line?

+6
source share
4 answers

The const keyword, shown after the function, guarantees the caller of the function that no member data variables will be changed.

For example, given this class,

 // In header class Node { public: Node(); void changeValue() const; ~Node(); private: int value; }; 

// in .cpp

 void Node::changeValue() const { this->value = 3; // This will error out because it is modifying member variables } 

There is an exception to this rule. If you declare that the member data variable has been changed, then it can be changed regardless of whether the const function is declared. Using mutable is a rare situation when an object is declared persistent, but in practice there are member data variables that need options to change. One possible use case is to cache a value that you might not want to repeat the original calculation. This is usually a rarity ... But it's good to know about it.

For example, given this class,

 // In header class Node { public: Node(); void changeValue() const; ~Node(); private: mutable int value; }; 

// in .cpp

 void Node::changeValue() const { this->value = 3; // This will not error out because value is mutable } 
+4
source

The constant says that the function will not change any of the this data elements unless they are marked as mutable.
Only a member function can be marked as const, so this means that none of the members will be changed inside the function.

+7
source

This is a “defensive programming” method that helps protect your own programming errors. With const from a function parameter, you declare that the function should not change this parameter, and adding const causes the compiler to prevent you from inadvertently doing this. Similarly, if you write a member function that should not change any member variables of your class, then you can declare the entire const function like this, and that will stop you from doing this.

It also helps make your code self-documenting. Adding the const parameter to the parameter tells the user that "this function does not change this parameter." Adding const to a member function tells the user that "this function does not modify any members of the class" (except for explicitly mutable ones).

Restricting access to anything, except when you really need to, should, as a rule, be considered good. This is the same reason you usually don’t log in as root, even if you can, and you will have more options if you did.

+4
source

The const keyword after the method means that the implicit this parameter (which is set to the address of the object used to call the method) indicates a constant object.

In C ++. a member function might look like this:

 class Foo { int x; mutable int y; public: void bar () { Foo *me = this; // * this is an implicit parameter // that points to the instance used // to call bar() assert(&x == &this->x); // * accesses to class members are // implicitly taken from this x = 1; // * can modify data members } void bar () const { // Foo *me = this; // * error, since "bar() const" means // this is a "const Foo *" const Foo *me = this; // * ok // x = 1; // * error, cannot modify non-mutable // members of a "const Foo" y = 0; // * ok, since y is mutable } }; 

An analog in C will function to access struct Foo * and a const struct Foo * , respectively:

 struct Foo { int x; int y; }; void Foo_bar (Foo *this) { /* ... */ } /* can modify this->x and this->y */ void cFoo_bar (const Foo *this) { /* ... */ } /* cannot modify this->x nor this->y */ 

There is no mutable analogue in C.

+2
source

Source: https://habr.com/ru/post/950138/


All Articles