2

I've read some answers in SO and tutorials in other places that generally give the following idea:

Reference variables can be used as a second name or an alias for another variable

Is that indeed true or are there situations where it is either not possible or not convenient to do so? When is it viable and how to make it so?


The motivation for my question:

I followed this notion inside variables of a class and ended up with a problem whenever objects of those classes were copied (The references inside the copied objects would refer to the original variables). The solution I've seen so far involves specifying a custom copy constructor to change the initialization of those aliases from the default, which can be a lot of work since you can't extend the default constructor to change those specific variables and you are then required to write one for your entire class or for a nested one that wraps your aliases ( and thus also limits the names you can use).

Bottom-line, as far as I know, using reference member variables as aliases is either unsafe (it won't work as expected if your variable is copied) or not easy( you may have to write and maintain a lot of code).

Having said that, my question can be split as follows:

  1. Can member reference variables be used as aliases without all the trouble mentioned earlier?
  2. Are there any other situations where they can be unsafe? ( besides in copy operations)
  3. When you don't have a member reference variable, can you indeed safely use them as a second name or is there a situation where extra care should be taken?
Community
  • 1
  • 1
guivenca
  • 159
  • 1
  • 11
  • Both your linked sources use references to local / global variables, and not to instance members. In your previous question you already outlined the issue caused by using references for instance members, and got a resultion. What is your question now then? – Amit Oct 08 '15 at 06:57
  • In the same cases in which you would safely and easily use a pointer – Paolo M Oct 08 '15 at 07:01
  • @Amit, I am asking if there is another situation ( besides instance members that are copied) where, if you follow the notion that reference variables can simply be used as aliases, you will face undesired behaviour. Also, the previous question I linked is for the case of a copy constructor, there may be other situations where you will get similar issues ( e.g. in a copy assignment operator,a destructor or maybe something outside the rule of 3 / 5) – guivenca Oct 08 '15 at 07:28

1 Answers1

2

References are safe when:

  1. What they are referencing cannot go away before the reference goes out of scope.

That's it.

Examples of this are:

A. Parameters to a function which will not store the references anywhere.

B. Aliasing a deeply nested or computed element in a container to make code cleaner.

C. Inside a functor that has temporary lifetime (such as a custom printer object)

At almost Any other time you should either be using a copy or a shared pointer.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142