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++?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
apb_developer
  • 321
  • 2
  • 9
  • Possible duplicate of [C++ Const Usage Explanation](http://stackoverflow.com/questions/5598703/c-const-usage-explanation) – Danh Aug 03 '16 at 09:30
  • none, the `const` qualifier can be before or after the type – EdChum Aug 03 '16 at 09:30
  • 3
    Possible duplicate of [const int = int const?](http://stackoverflow.com/questions/3247285/const-int-int-const) – LogicStuff Aug 03 '16 at 09:31

2 Answers2

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.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
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.

Krapow
  • 611
  • 6
  • 26