Created
August 25, 2014 17:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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