JMzance
JMzance

Reputation: 1746

'const' methods in template class in C++

I've just started reading about template classes in C++ and I have come across some syntax I'm unaware of. A class method is prototyped as:

template <class Type> class Range {
            ....
        bool Below    (const Type& value) const;
            ....
}

and defined as:

template <class Type> bool Range<Type>::Below(const Type& Value) const {

    if (Value < Lo) return true;
    return false;
}

Can anyone help me understand the meaning of the 'const' flag after the method inputs have been listed? I understand there use when put before an input but not after. Cheers, Jack

Upvotes: 1

Views: 359

Answers (4)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

In const member functions a top level const qualifier is applied to each member of the class, unless the member is marked as mutable (which means never const).

You can also have volatile member functions and both volatile and const.

Note, that for pointers and references the behaviour may be surprising:

struct X {
    int a;
    int* pa;
    int& ra;
    X() 
        : a(1)
        , pa(&a) 
        , ra(a)
    {}

    void foo() const {
        *pa = 2; // Ok, this is the pointer being const, not the value being pointed to.
        ra = 3; // Ok, this is the reference being const, not the value being referenced.
        a = 4; // Error, a is const
        pa = &a; // Error, pa is const.
    }
};

Below is how top level const qualifier is applied:

  • int becomes int const - a constant integer.
  • int* becomes int* const - a constant pointer, not int const* - a pointer to constant.
  • int& becomes int& const - a constant reference, not int const& - a reference to constant. Applying const to references does not do anything, because they can not be changed to refer to another object anyway.

Another way to think about this, is that in non-const member functions this has the type of X* const, whereas in const member functions this is X const* const. Note how this pointer is always constant.

Upvotes: 9

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

Compilers implicitly pass a pointer to the object as an argument of a non-static member. It is named this in the function definition. The qualifier const means that this pointer this has the qualifier const. That is it is applied to the pointer this.

Upvotes: 1

nvoigt
nvoigt

Reputation: 77285

This means the method is const. The method cannot change the state of the object it is called upon. If you change variables in this method, you will get a compiler error.

Only methods declared const can be called on a const object, because they are guaranteed to not change it.

Upvotes: 3

Matt Holmes
Matt Holmes

Reputation: 1065

Const after a method means that the method itself is constant, and in fact will have a constant 'this' pointer. In basic terms it means that method is not allowed to make changes to the state of the class (member variables). This allows the compiler to optimize on this fact, and tells other developers that the method will not be making state changes.

There are ways to get around this rule in C++, such as the 'mutable' keyword and const_cast, but really you shouldn't try and break const correctness unless you really, really know what you're doing.

Upvotes: 3

Related Questions