Examples of Value-based and Reference-based Collections
Let's look at two code fragments that demonstrate the difference between the value-based and the reference-based procedures.
First, a value-based example:
/* A vector of RWCStrings:
*/RWTValOrderedVector<RWCString> v;
RWCString s("A string");
v.insert(s);
And a reference-based example:
/* A vector of pointers to RWCStrings:
*/RWTPtrOrderedVector<RWCString> v;
RWCString* p = new RWCString("A string");
v.insert(p);
Both code fragments insert an
RWCString into vector
v.
In the first example,
s is an
RWCString object containing
A string. The statement
v.insert(s) copies the value of
s into the vector. The object that lies within the vector is distinct and separate from the original object
s.
In the second example,
p is a pointer to the
RWCString object. When the procedure
v.insert(p) is complete, the new element in
v will refer to the same
RWCString object as
p.