1

I'm learning C++ from "C++ for Programmers" book. In "templates" section, there is a code like that:

template<typename T>
void printArray(const T * const array, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << array[i] << " ";
    }

    cout << endl;
}

My question is, the constants of first parameter in function. I have never seen two constants in one parameter. I tried to realize but I could not. Thanks for helps.

Casey
  • 41,449
  • 7
  • 95
  • 125
Firat Akandere
  • 523
  • 2
  • 12
  • 1
    The first `const` means that you won't be allowed to modify the pointed object through that pointer, while the second `const` means that you won't be allowed to modify the pointer itself (e.g. make it point to a different object, set it to `nullptr`, etc.) – Andy Prowl Jul 22 '13 at 23:41
  • 1
    const pointer to type T which is also const? – Rapptz Jul 22 '13 at 23:41

3 Answers3

4

const T * const means: a constant pointer to a constant T. Which means that both the pointer and the T pointed are constant.

A good rule for reading this kind of parameters is to read it right to left.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I think you reversed it twice. Once by rearranging in reverse order, and then by reading it right-to-left. Do one or the other but not both. – Ben Voigt Jul 23 '13 at 00:01
  • @BenVoigt, yeah, I've that messed up. It's easy to read it in your mind, but hard to put it down in English. – Shoe Jul 23 '13 at 00:02
1

In your example, you have a const pointer to a const object of type T. This means you can't change where the pointer is pointing or the object to which it points.

You can actually have const in even more places in a single line. Take this declaration for example:

class MyClass {
public:
    const std::string& get_name(const int * const id) const;
};

In this case, the function get_name is constant, and can't modify the instance of MyClass. It takes in a constant pointer to a constant integer, and returns a constant reference to a string.

If you'd like to learn more about best practices while using const (and other parts of C++), I highly recommend Bruce Eckel's book Effective C++: 55 Specific Ways to Improve Your Programs and Designs.

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
  • `T` is not necessary an object. – Shoe Jul 22 '13 at 23:43
  • @Jeffrey I don't think it can be a function or reference, so how can `T` not be the type of an object? – dyp Jul 22 '13 at 23:46
  • @Jeffrey An object can be a scalar type too. – Rapptz Jul 22 '13 at 23:47
  • @Rapptz, but a scalar type is not necessary an object. – Shoe Jul 22 '13 at 23:48
  • @Jeffrey In the Standard, "object" is just a region of storage with some properties. E.g. there can be objects of type `int`. In this sense of "object", all "instances" of scalar types are objects. – dyp Jul 22 '13 at 23:50
  • 1
    @Jeffrey 1.8.1 of the C++11 standard says: An *object* is a region of storage. So yes, a scalar type is indeed an object since it takes up storage. – Rapptz Jul 22 '13 at 23:51
  • @derekerdmann, not it's not worth a downvote, that's why I undid it. – Shoe Jul 22 '13 at 23:51
0

The syntax just tells you that there is a pointer that is const and the thing it points to is also const.

For more reading I would recommend you have a look at:

http://www.parashift.com/c++-faq/const-ptr-vs-ptr-const.html

http://www.parashift.com/c++-faq/const-correctness.html

shuttle87
  • 15,466
  • 11
  • 77
  • 106