Skip to content

Instantly share code, notes, and snippets.

@theidexisted
Created May 22, 2016 13:52
Show Gist options
  • Save theidexisted/775487481094ab4479dcc6507ade957d to your computer and use it in GitHub Desktop.
Save theidexisted/775487481094ab4479dcc6507ade957d to your computer and use it in GitHub Desktop.
watch out for return value
class Shape {
public:
virtual ~Shape() { } // A virtual destructor
virtual void draw() = 0; // A pure virtual function
virtual void move() = 0;
// ...
virtual Shape* clone() const = 0; // Uses the copy constructor
virtual Shape* create() const = 0; // Uses the default constructor
};
class Circle : public Shape {
public:
Circle() = default;
~Circle() = default;
void draw() {}
void move() {}
Circle* clone() const; // Covariant Return Types; see below
Circle* create() const; // Covariant Return Types; see below
// ...
};
Circle* Circle::clone() const { return new Circle(*this); }
Circle* Circle::create() const { return new Circle(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment