I suspect that you have bugs in your program in code that you did not show.
To illustrate this, compile and run this program:
Code:
#include <iostream>
using namespace std;
void Swap(int& num1, int& num2)
{
cout<<"Before swap:"<<num1<<" "<<num2<<endl;
int& temp=num1;
num1=num2;
num2=temp;
cout<<"After swap:"<<num1<<" "<<num2<<endl;
}
int main()
{
int n1(13), n2(11);
Swap(n1,n2);
cout<<n1<<" "<<n2<<endl;
}
You should get this output:
Code:
Before swap:13 11
After swap:11 11
11 11
The reason is that the reference can be viewed as an alias, so the Swap function above is equivalent to:
Code:
void Swap(int& num1, int& num2)
{
cout<<"Before swap:"<<num1<<" "<<num2<<endl;
num1=num2;
num2=num1;
cout<<"After swap:"<<num1<<" "<<num2<<endl;
}
which obviously makes both num1 and num2 have the same value, i.e., the value of num2 when the function is called.
Your code, on the other hand, does something different, so my explanation is that you have a bug elsewhere too.
EDIT:
Also, I am assuming that Number is an (abstract) base class, and besides Int, you might have other subclasses, e.g., Double. In that case, what does it mean to swap an Int and a Double? Maybe you are just better off defining a swap function for each subclass, if it can do better than std::swap.