Baby Dolphin
Baby Dolphin

Reputation: 489

const definition doubts

Does it make any difference to define something as

const string msg=_T("serious");
const string& msg=_T("serious");

and

string const msg=_T("serious");
string const&msg=_T("serious");

Thank you

Upvotes: 0

Views: 90

Answers (2)

hamstergene
hamstergene

Reputation: 24429

There is no difference between const string and string const.

const string msg creates msg object in local scope. This is what you should do.

const string& msg creates a reference which is initialized with temporary object. Here, lifetime of the temporary object is prolonged until msg goes out of scope, so it's ok, but in other expressions it may be a error or undefined behavior. If you drop const, the definition will no longer be valid. You shouldn't use this.

You do not need _T() here, as string will not turn into wstring in Unicode builds.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206518

There is No difference both of them are same. It is just different style of saying so.

Upvotes: 2

Related Questions