Yes, the pass by reference is important. However, the standard output does not belong in the function swap. Let the function do only what its name implies.
Anyway, here is how it looks when more people work at the same project in real life:
Code:
#include <iostream>
#include <string>
namespace stacy
{
//swap() swaps two strings.
void swap(std::string& string1, std::string& string2)
{
std::string holder;
holder = string1;
string1 = string2;
string2 = holder;
}
}
namespace Alex
{
//swap() swaps two strings.
void swap(std::string* string1, std::string* string2)
{
std::string holder;
holder = *string1;
*string1 = *string2;
*string2 = holder;
}
}
int main()
{
std::string one("one"), two("two"), three("three");
//Entering namespace scope.
{
using namespace stacy;
swap(one, two);
}
//Exiting namespace scope
//---------------------------------//
//Entering namespace scope.
{
using namespace Alex;
swap(&two, &three);
}
//Exiting namespace scope
std::cout << "After swap: " << one << " " << two <<" "<< three << std::endl;
std::cin.get();
}