In short, stack is a typedef for reference to a Rep type. This means that whenever you use stack , you specify the type of "link to Rep".
In my opinion, this is strange material from the C ++ course. From what the interface looks like, we can assume that the implementation will be something like
stack create() {
Now, if you do the following, you violate the principle of least surprise for most C ++ programmers.
stack p = create(); stack p2 = p;
The principle of least surprise says this copies the stack. But in fact, stack denotes the Rep& type, which is a "reference to Rep". This means that the second line creates an alias p (a reference to what p means). It does not copy, but simply creates another link.
I recommend you: do not allow such a code. If you want to hide the implementation or layout of Rep , then follow the Pimpl idiom .
source share