Skip to content

Instantly share code, notes, and snippets.

@mloskot
Created January 13, 2012 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloskot/1607156 to your computer and use it in GitHub Desktop.
Save mloskot/1607156 to your computer and use it in GitHub Desktop.
Moveable semantic fun
#include <iostream>
#include <string>
#include <utility>
#include <boost/noncopyable.hpp>
struct animal : private boost::noncopyable
{
animal() {}
animal(char const* name) : name(name) {}
animal(animal&& a) : name(a.name)
{
a.name.clear();
}
animal& operator=(animal&& a)
{
name = a.name;
a.name.clear();
return *this;
}
std::string name;
};
struct cat : public animal
{
cat(char const* name) : animal(name) {}
};
struct dog : public animal
{
dog(char const* name) : animal(name) {}
};
struct Cage
{
private:
animal animal_;
public:
void set_animal(animal&& a)
{
animal_ = std::move(a);
}
std::string who() const { return animal_.name; }
};
int main()
{
using std::cout;
using std::endl;
Cage cage;
cage.set_animal(cat("Kitty"));
std::cout << cage.who() << std::endl;
cage.set_animal(dog("Bamboozle"));
std::cout << cage.who() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment