Created
May 25, 2018 02:34
-
-
Save thara/61f9e6f08d5c2a2bbec611e3ca015f38 to your computer and use it in GitHub Desktop.
cocos2d::Ref template factory function
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
| #include <iostream> | |
| namespace cocos2d { | |
| class Ref | |
| { | |
| public: | |
| Ref* autorelease() { | |
| return this; | |
| }; | |
| }; | |
| } | |
| template<typename T, typename... Args> | |
| T* create(Args &&... args) | |
| { | |
| static_assert(std::is_base_of<cocos2d::Ref, T>::value, "T must be a descendant of cocos2d::Ref"); | |
| T* obj = new T(std::forward<Args>(args)...); | |
| obj->autorelease(); | |
| return obj; | |
| } | |
| class A : public cocos2d::Ref {}; | |
| class B : public cocos2d::Ref { | |
| public: | |
| explicit B(int a) { | |
| std::cout << "B:" << a << std::endl; | |
| }; | |
| explicit B(int a, int b) { | |
| std::cout << "B:" << a << "," << b << std::endl; | |
| }; | |
| }; | |
| int main() { | |
| std::cout << "Hello, World!" << std::endl; | |
| A* a = create<A>(); | |
| B* b1 = create<B>(10); | |
| B* b2 = create<B>(10, 20); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment