Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ const question

Tags:

c++

gcc

constants

If I do this:

// In header 
class Foo {
void foo(bar*);
};

// In cpp
void Foo::foo(bar* const pBar) {
//Stuff
}

The compiler does not complain that the signatures for Foo::foo do not match. However if I had:

void foo(const bar*); //In header
void Foo::foo(bar*) {} //In cpp

The code will fail to compile.

What is going on? I'm using gcc 4.1.x

like image 597
anio Avatar asked Nov 06 '08 19:11

anio


People also ask

What is a const in C?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

Why do we use const in C?

We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value of the value of PI, you wouldn't like any part of the program to modify that value.

What is const int in C?

const int* const says that the pointer can point to a constant int and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant int.

Can we change const variable in C?

In C or C++, we can use the constant variables. The constant variable values cannot be changed after its initialization.


2 Answers

In the first, you've promised the compiler, but not other users of the class that you will not edit the variable.

In your second example, you've promised other users of the class that you will not edit their variable, but failed to uphold that promise.

I should also note that there is a distinct difference between

bar* const variable

and

const bar* variable

and

const bar* const variable

In the first form, the pointer will never change, but you can edit the object that is pointed to. In the second form, you can edit the pointer(point it to another object), but never the variable that it points to. In the final form, you will neither edit the pointer, nor the object it points to. Reference

To add a bit more of a clarification to the question stated, you can always promise MORE const than less. Given a class:

class Foo {
    void func1 (int x);
    void func2 (int *x);
}

You can compile the following implementation:

Foo::func1(const int x) {}
Foo::func2(const int *x) {}

or:

Foo::func1(const int x) {}
Foo::func2(const int* const x) {}

without any problems. You've told your users that you may possibly edit their variables. In your implementation, you've told the compiler that this particular implementation will not edit those variables, even though the told the users you might. You haven't broken a promise to the user, and so the code compiles.

like image 92
Douglas Mayle Avatar answered Oct 11 '22 18:10

Douglas Mayle


See this question, this question, and this question.

Basically, the const only means that the function will not modify the pointer's value. The pointers contents are not const, the same as the header's signature.

like image 27
Greg Rogers Avatar answered Oct 11 '22 18:10

Greg Rogers