Using 'Const' in Class's Functions

Using 'const' in class's functions

A const method can be called on a const object:

class CL2
{
public:
void const_method() const;
void method();

private:
int x;
};

const CL2 co;
CL2 o;

co.const_method(); // legal
co.method(); // illegal, can't call regular method on const object
o.const_method(); // legal, can call const method on a regulard object
o.method(); // legal

Furthermore, it also tells the compiler that the const method should not be changing the state of the object and will catch those problems:

void CL2::const_method() const
{
x = 3; // illegal, can't modify a member in a const object
}

There is an exception to the above rule by using the mutable modifier, but you should first get good at const correctness before you venture into that territory.

Const Class and member functions

Yes, if an object is const, you can only call const functions on that object.

If a function is not marked const, the compiler must assume that it is allowed to change the members of the class. And since you can't change the members of a const class instance, you cannot call non-const functions on that instance either.

Note that the opposite case is different - there is no problem calling const functions on non-const objects. Therefore it is recommended to always mark member functions as const wherever it makes sense to do so. It is important to mark function parameters, reference variables, etc. as const for many of these same reasons. The community typically refers to this practice as maintaining "const correctness."

What is meant with const at end of function declaration?

A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.

Another way of thinking about such "const function" is by viewing a class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of member variables are possible.

It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)

As usual when dealing with the const keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const only applies when adding const to the end of the function declaration after the parenthesis.

const is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const correctness and the const keyword:

Const correctness

The C++ 'const' Declaration: Why & How

Why can I modify the class with a const function in C++11?

It is because you do not modify it. When you do:

int* tab

tab contains only an address. Then in

void DArray::setValue(unsigned n, int value) const // HERE
{
tab[n] = value;
}

You do not modify this address, you modify some memory after it. Thus you do not modify your class.

If instead, you used

std::vector<int> tab

You would have an error in setValue because you would modify an element of your class.

Why use the keyword 'const' twice in a class member function C++

The first const qualifies the returned value as non-modifiable; the second const specifies that the function does not modify the class instance (or any member of it) on which it was called.

The first is more often used when the function returns a reference to a member, to prevent that returned reference from being used to modify the member to which it refers.

Does a class with all attributes const need to have member function declared const as well?

If you do not declare the member functions as const, you can't invoke them on const objects or objects referenced by a const reference. For example, the following won't work:

const PermutationGroup& group = PermutationGroup("foobar", 42, ...);
group.check_sgs(); // ERROR: can't invoke non-const function on const objects

c++ constant function in class

It means exactly nothing. The function does not return anything so the const modifier modifies nothing.



Related Topics



Leave a reply



Submit