0

I have read about use cases of const and I feel like I have a good understanding of const for the most part. However, I can't seem to figure out why I don't see this more often:

void someFunction(const string& A) const

Where you have a const parameter in a const member function. For some reason, whenever I look up examples and the function is const, the const seems to be stripped off the parameters like this:

void someFunction(string& A) const

However that doesn't seem to stop me from modifying A. Is it considered bad form to have const parameters in a const member function?

What is the reasoning for not keeping the const in the parameters as well if A would not be modified?

EDIT: This is my fault for not clarifying but I understood the difference between adding it before the parameter and adding it after the function. A lot of code I looked at just never combined the two and I was just trying to figure out if there was a reason for that.

Nabren
  • 582
  • 1
  • 7
  • 17
  • possible duplicate of [Const correctness: const char const \* const GetName const (//stuff);](http://stackoverflow.com/questions/1835399/const-correctness-const-char-const-const-getname-const-stuff) – Jerry Coffin Oct 09 '13 at 23:40

2 Answers2

5
void someFunction(const string& A) const

The last const means that the method will not change the state of the object referenced by *this inside it. The first const is saying that the function will not change the state of the argument - and it doesn't have any correlation with the second const, so you may have this:

void someFunction(string& A) const

In this case function may change state of A argument, but it may not change the state of its object.

For example (and this is a highly hypothetical example):

class MyIntArray
{
 // some magic here in order to implement this array    

public:
 void copy_to_vector(std::vector<int> &v) const
 {
    // copy data from this object into the vector.
    // this will modify the vector, but not the
    // current object.
 }

}

And this is the example where these two are combined:

class MyOutput
{
    char prefix;
    // This class contains some char which
    // will be used as prefix to all vectors passed to it
public:
     MyOutput(char c):prefix(c){}
     void output_to_cout(const std::vector<int> &i) const
     {
        // iterate trough vector (using const_iterator) and output each element
        // prefixed with c - this will not change nor the vector
        // nor the object.
     }

}

Oh, and take a look into this question: Use of 'const' for function parameters

Community
  • 1
  • 1
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • That's what I was figuring. However, I just haven't seen them used together in the examples I have been able to find and was just trying to figure out if there was a reason for that. It may just be that the examples were just kept simple and didn't combine them, but I tried to see if there was a reason I never saw it that way. This clarifies my assumption. Thanks. – Nabren Oct 09 '13 at 23:37
  • @Nabren I've added the small example (the only I could think of in 1:41AM). – Nemanja Boric Oct 09 '13 at 23:41
  • Ya, I understand that completely. I guess I can't even get the question out right. Put it this way, I always see it written like this: "void copy_to_vector(std::vector &v) const" Even in cases where v is not modified. So I was wondering why it just wouldn't be written like this: "void copy_to_vector(const std::vector &v) const" Is all. From what I can gather from the answers here, it should. I just find it odd I don't see it like that more often if that's the case. – Nabren Oct 09 '13 at 23:45
  • Yeah, you're right, if the argument (reference or pointer) is not modified, it should be written with `const` specifier . – Nemanja Boric Oct 09 '13 at 23:49
2

const on a function only applies to member functions. It states that the class object will not be modified. It does not mean that a function parameter passed by reference cannot be modified.

Passing a parameter to a function as const & prevents you from modifying the parameter.

class A
{
private:
    int data;
public:
    void func1(const std::string& s) const; // s cannot be modified, members of A cannot
    void func2(std::string& s) const; // s can be modified, members of A cannot

    void func3(const std::string& s); // s cannot be modified, members of A can be
    void func4(std::string& s); // s can be modified, as can members of A
};

The functions labeled const cannot change data, but no such restriction is on s unless it is labeled const.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42