1

I am using pass by reference to const string in a function argument like below:

class Super
{
   void alignment(const string & str);
};

void Super::alignment(const string & str)
{
  //do some stuff
}

Now if I use as below it gives the same result:

void Super::alignment(string const & str)
{
  //do some stuff
}

What is the difference in this two types of definition in c++?

3
  • Possible duplicate of C++ Const Usage Explanation Commented Aug 3, 2016 at 9:30
  • none, the const qualifier can be before or after the type Commented Aug 3, 2016 at 9:30
  • 3
    Possible duplicate of const int = int const? Commented Aug 3, 2016 at 9:31

2 Answers 2

7

const T& and T const& are semantically the same exact thing. Just like const T* and T const* are semantically the same, but T const* and T* const are different.

Sign up to request clarification or add additional context in comments.

Comments

4

the const keyword is left associative, but in "const string &str", since there's no left keyword to qualify, it apply to the word at his right (so string).

It is the same but it's not parsed the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.