Hi jimmyjrosu,
There are two parts to the answer: creating a template function and
creating an assignment operator.
As mathtalk suggested and you may already know, a template function
can work with various types. In your case, it might look like this:
template <class T> void swap(T& x, T& y)
{
T temp;
// code as in your question
}
The use of templates is a big topic, about which whole books have been
written, and I suspect from your reference to instantiation that you
already know about them, so I will not go into further explanation
here.
The other part of the answer is how to define an assignment operator
for a class. An assignment operator has to be a class member
function, usually declared like this:
class Fred
{
public:
Fred& operator =(const Fred& rhs);
};
In the function definition, you do whatever is appropriate to copy the
content of rhs to the current object (*this). ("rhs" is a traditional
name for the argument, standing for "right-hand side.") If a class
just contains plain old data, this is straightforward; if the class
contains pointers, it's trickier.
If you don't declare an assignment operator, the compiler will
generate one for you, but if the class contains pointers, the
compiler-generated operator may not do what you need.
C++ gurus recommend that an assignment operator return *this, because
that is how built-in assignment operators work. This is what enables
code like the following to work:
Fred a, b, c;
a = b = c;
The "b = c" part returns a reference to b, which is the right-hand
side of the assignment to a.
Another common bit of advice is to check for assignment to self. This
avoids a possible problem where the assignment operator first trashes
the assignment target's content to make room for the new content, then
can't get the content from the rhs object because it's the same
object.
For more on assignment operators, see:
http://leepoint.net/notes/cpp/oop-overloading/overloadassign.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/HTML/_pluslang_assignment.asp
Having said all that, I should note that you don't need to write the
swap function because it is already in the standard library. If you
use that function, you may need to define a copy constructor as well
as an assignment operator for your stack class. (My reference on the
standard library says you do need a copy constructor, but the
implementation of the swap template in the compilers I have doesn't
use the copy constructor.) But in general, any time you need an
assignment operator, it is a good idea to define a copy constructor
too.
The swap template function is in the <algorithm> header file. For a
description, see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfalgorithmswap.asp
Search Strategy
The basic answer came from my knowledge of C++. I found the web pages
referenced via searches for
C++ assignment operator
C++ algorithm library reference
I also referred to a couple of books:
C++ FAQs by Marshall Cline, Greg Lomow, and Mike Girou.
Addison-Wesley (2nd ed., 1999)
The Standard C++ Library: A Tutorial and Reference by Nicolai M.
Josuttis. Addison-Wesley (November 1999).
I hope this information is helpful. If you need more details about
any of this, please ask for a clarification.
--efn |