0

I have to name windows with constant literal strings. The names will be referenced everywhere so I need to cache them with variables. My question is

Do I need to declare the variables as const references as follows

const string& srcWnd = "Source Window";

instead of

const string srcWnd = "Source Window";

?

Is there any difference?

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165

1 Answers1

2

I don't see any significant difference between the two, but I suggest using the plain string (option 2):

const string srcWnd = "Source Window";

You don't win anything by using a reference. And if you use a reference, the code becomes more obscure, because by using a reference you rely on temporary lifetime extension. If someone doesn't remember lifetime extension rules clearly, they might need to look them up just to be sure that the code is valid.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207