简体   繁体   中英

What is the difference between const string & str and string const & str when passed as an argument to a function in c++

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM