CIS 3142 — Aliasing in Java and C++

CISC 3142
Programming Paradigms in C++
Aliasing in Java and C++

Aliases in Java

Here is an example scenario that illustrates a typical aliasing scenario in Java:
Vector v1 = new Vector();
…		// populate the vector
Vector v2 = v1;
v1.add(7);
v2.add(3);

Improper Aliasing in C++

Here is an example scenario that illustrates the consequences of improper aliasing in C++:
vector v1;
…
vector v2 = v1;
v1 += 7;
v2 += 3; 
v1 += 8;

Maintaining Proper Value Semantics in C++

Here is an example scenario that illustrates the proper of objects in C++:
vector v1;
…
vector v2 = v1;
v1 += 7;

Proper Aliasing in C++ — Using Reference Semantics

Here is a parallel example to the one above, but uses pointers and reference semantics: