Skip to content

Instantly share code, notes, and snippets.

@mem
Created August 25, 2014 17:34
Show Gist options
  • Save mem/a960a1ae48b68fb9317f to your computer and use it in GitHub Desktop.
Save mem/a960a1ae48b68fb9317f to your computer and use it in GitHub Desktop.
NeedsObject needs an instance of Object. In order to manage the scope of the Object instance, a class is derived from NeedsObject and it's put in charge of the Object instance lifetime. This allows the creation of another class for testing purposes.
class Object {
public:
Object() { }
virtual ~Object() { }
};
class NeedsObject {
protected:
NeedsObject(Object *object) : object(object) { }
~NeedsObject() { }
protected:
void setup() { }
void shutdown() { }
private:
NeedsObject();
Object * object;
};
template <class ObjectType>
class NeedsObjectContainer : public NeedsObject {
public:
NeedsObjectContainer() : NeedsObject(&object) {
setup();
}
~NeedsObjectContainer() {
shutdown();
}
private:
ObjectType object;
};
typedef NeedsObjectContainer<Object> MyNeedsObject;
class MockObject : public Object {
public:
MockObject() { }
virtual ~MockObject() { }
};
typedef NeedsObjectContainer<MockObject> TestNeedsObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment