ä»ãã1幎éãç§ã¯æãªãšãã«MavenãšSpring for C ++ã®æ··åç©ã飲ãã§ããŸãã ãã®éèŠãªéšåã¯ã ã¹ããŒããã€ã³ã¿ãŒã®èªå·±èšè¿°ã·ã¹ãã ã§ãã ãªãããããã¹ãŠå¿ èŠãªã®ãã¯å¥ã®åé¡ã§ãã ãã®èšäºã§ã¯ãäžèŠäŸ¿å©ãªC ++ã®æ©èœã®1ã€ãã©ã®ããã«æšæºã®åžžèãçããããã«ã€ããŠç°¡åã«èª¬æããããšæããŸãã
ç·šéè ïŒ
habrasocietyãšStandardã«è¬ããŸãã èšäºãæçš¿ãããç¿æ¥ãç§ã¯èªåã®èãã«å€§ããªèª€ããããããšã«æ°ã¥ããŸããã ããã«èšäºã®çµãããèªãæ¹ãè¯ãã§ããã...ãããŠãã¯ãã ã³ããŒã®çç¥ ãããã¯çµå±ãèšäºã¯éæ¥çã«é¢é£ããŠããã ãã§ãã
1.åé¡
ãããžã§ã¯ãã®ã¹ããŒããã€ã³ã¿ãŒã¯æšå¹Žã®å€ã«äœæãããŸããã
éžæããããã€ã³ã¿ãŒã³ãŒããšèª¬æ
ãªããžã§ã¯ãã¹ãã¬ãŒãžã®ããžãã¯ãšãªããžã§ã¯ããžã®ã¢ã¯ã»ã¹ã®ããžãã¯ãå®è£ ããæŠç¥æ§é ããããŸãã ãããã®åããã³ãã¬ãŒãåŒæ°ãšããŠã¹ããŒããã€ã³ã¿ãŒã¯ã©ã¹ã«æž¡ããŸãã IDSharedMemoryHolder-ãªããžã§ã¯ãã¡ã¢ãªã¢ã¯ã»ã¹ã€ã³ã¿ãŒãã§ã€ã¹ã retainïŒïŒé¢æ°ã®åŒã³åºãã«ãããã¹ããŒããã€ã³ã¿ãŒããªããžã§ã¯ãã®ææãéå§ããŸãïŒåŒ·åç §ã®å Žåã¯++ ref_count ïŒã releaseïŒïŒãåŒã³åºãããšã«ããããã€ã³ã¿ãŒã¯ãªããžã§ã¯ãã解æŸããŸãïŒåŒ·åç §--ref_countããã³ref_count == 0ã®å Žåã¯ãªããžã§ã¯ããåé€ããããïŒã
åç §è§£é€ã«é¢é£ãããã®ãæå³çã«çç¥ãããªãã¬ãŒã¿ãŒåŒã³åºãã§ããã«ä¿æããŸãã ãããã®ç¹ã«ã€ããŠèª¬æããåé¡ã¯é¢ä¿ãããŸããã
template<typename T_Type, typename T_Holding, typename T_Access> class DReference { . . . IDSharedMemoryHolder *_holder; void retain(IDSharedMemoryHolder *inHolder) { . . . } void release() { . . . } . . . ~DReference() { release(); } template<typename T_OtherType, typename T_OtherHolding, typename T_OtherAccess> DReference( const DReference<T_OtherType, T_OtherHolding, T_OtherAccess> & inLReference) : _holder(NULL), _holding(), _access() { retain(inLReference._holder); } . . . }
ãªããžã§ã¯ãã¹ãã¬ãŒãžã®ããžãã¯ãšãªããžã§ã¯ããžã®ã¢ã¯ã»ã¹ã®ããžãã¯ãå®è£ ããæŠç¥æ§é ããããŸãã ãããã®åããã³ãã¬ãŒãåŒæ°ãšããŠã¹ããŒããã€ã³ã¿ãŒã¯ã©ã¹ã«æž¡ããŸãã IDSharedMemoryHolder-ãªããžã§ã¯ãã¡ã¢ãªã¢ã¯ã»ã¹ã€ã³ã¿ãŒãã§ã€ã¹ã retainïŒïŒé¢æ°ã®åŒã³åºãã«ãããã¹ããŒããã€ã³ã¿ãŒããªããžã§ã¯ãã®ææãéå§ããŸãïŒåŒ·åç §ã®å Žåã¯++ ref_count ïŒã releaseïŒïŒãåŒã³åºãããšã«ããããã€ã³ã¿ãŒã¯ãªããžã§ã¯ãã解æŸããŸãïŒåŒ·åç §--ref_countããã³ref_count == 0ã®å Žåã¯ãªããžã§ã¯ããåé€ããããïŒã
åç §è§£é€ã«é¢é£ãããã®ãæå³çã«çç¥ãããªãã¬ãŒã¿ãŒåŒã³åºãã§ããã«ä¿æããŸãã ãããã®ç¹ã«ã€ããŠèª¬æããåé¡ã¯é¢ä¿ãããŸããã
ã¹ããŒããã€ã³ã¿ãŒã®åäœã¯ãäžé£ã®ç°¡åãªãã¹ãã«ãã£ãŠãã¹ããããŸãããããã€ã³ã¿ãŒã«é¢é£ä»ãããããªããžã§ã¯ããäœæãããã€ã³ã¿ãŒã«ãã€ã³ã¿ãŒãå²ãåœãŠãŠãreatin / releaseãæ£ããåäœããããã«èŠããŸãããã ãã¹ãïŒä»ã§ã¯éåžžã«å¥åŠã«æããŸãïŒã«åæ ŒããŸããã 1æäžæ¬ã«ã³ãŒããã¹ããŒããã€ã³ã¿ãŒã«å€æããŸããã
åé¡ã¯ãã¹ããŒããã€ã³ã¿ãŒã«ãã£ãŠå¶åŸ¡ãããã¡ã¢ãªãäºåã«åé€ãããŠããããšãçºèŠããã1ãæåã«å§ãŸããŸããã
å ·äœäŸãæããŠèª¬æããŸãã
DStrongReference<DPlugIn> DPlugInManager::createPlugIn( const DPlugInDescriptor &inDescriptor) { . . . DStrongReference<DPlugIn> thePlugInReference = internalCreatePlugIn(inDescriptor); . . . return thePlugInReference; } ... DStrongReference<DPlugIn> DPlugInManager::internalCreatePlugIn( const DPlugInDescriptor &inDescriptor) { for (IDPlugInStorage *thePlugInStorage : _storages) { if (thePlugInStorage->getPlugInStatus(inDescriptor)) return thePlugInStorage->createPlugIn(inDescriptor); } return DStrongReference<DPlugIn>(); } ... class DPlugInStorageImpl : public IDPlugInStorage { public: virtual ~DPlugInStorageImpl() { } virtual DStrongReference<DPlugIn> createPlugIn( const DPlugInDescriptor &inDescriptor); };
DPlugInStorageImpl :: createPlugInïŒ...ïŒã¡ãœãããåŒã³åºããããšããªããžã§ã¯ããDStrongReferenceãä»ããŠè¿ãã ããã®åŸããã®ã¹ããŒããã€ã³ã¿ãŒãDPlugInManager :: internalCreatePlugInïŒ...ïŒã¡ãœãããä»ããŠåŒã³åºãã³ã³ããã¹ãã«è¿ãããŸã-DPlugInManager :: createPlugInïŒ...ïŒã¡ãœããã
ãã®ãããã¹ããŒããã€ã³ã¿ãŒãDPlugInManager :: createPlugInïŒ...ïŒ ã¡ãœããã«è¿ãããå Žå ã PlugInReferenceã¯ãªã¢ãŒããªããžã§ã¯ããæããŠããŸããã æããã«ãä¿æ/解æŸã®åŒã³åºãã®æ°ãééã£ãŠããŸããã Eclipseã®ãããã¬ãŒã§å€ãã®ç¥çµãè²»ãããŠïŒãšããã§-ããã¯ã²ã©ãã§ãïŒãç§ã¯åãåºããç°¡åãªæ¹æ³ã§åé¡ã解決ããŸãã-ç§ã¯ãã°ã䜿çšããŸããã ç§ã¯åºåãretainã¡ãœãããšreleaseã¡ãœããã®åŒã³åºãã«çœ®ããããã°ã©ã ãèµ·åããŸãã...äœãæåŸ ããŠããŸãããïŒ ããã«äœãããããŸãïŒæ¬äŒŒã³ãŒãïŒïŒ
DPlugInStorageImpl :: createPlugInïŒ...ïŒ=> RETAIN
DPlugInManager :: internalCreatePlugInïŒ...ïŒãcreatePlugIn => RETAINãè¿ããŸã
DPlugInStorageImpl :: createPlugInïŒ...ïŒããDStrongReferenceïŒïŒ=> RELEASE
DPlugInManager :: createPlugInïŒ...ïŒãthePlugInReference = internalCreatePlugInïŒ...ïŒ=> RETAIN
DPlugInManager :: internalCreatePlugInïŒ...ïŒããDStrongReferenceïŒïŒ=> RELEASE
åèšïŒ thePlugInReferenceã®ref_count = 1 ãã¹ãŠãæ確ã«ããå¿ èŠããããŸããã
ç§ãå®éã«èŠãã®ã¯ããã®ããã«ïŒ0_0ïŒããããããçš®é¡ã®ã¯ãªãŒã³ã¢ãããåã³ã³ãã€ã«ãæé©åèšå®ã®å確èªãstdoutã®ãã©ãã·ã¥ã®è©Šè¡ãªã©ãè¡ãããã«æ¬¡ã®
DPlugInStorageImpl :: createPlugInïŒ...ïŒ=> RETAIN
DPlugInManager :: internalCreatePlugInïŒ...ïŒããDStrongReferenceïŒïŒ=> RELEASE
ããã«ã³ãŒãã®åé¡ã解決ããããã«å¿ æ»ã§ããã§ã«éåžžã«ééã£ãäœããçã£ãŠããã®ã§ãå°ããªãã¹ããããžã§ã¯ããäœæããŸããã
2.ãã¹ã
ãã¹ãã³ãŒãïŒ
#include <iostream> #include <stdio.h> class TestClass { private: int _state; public: TestClass(int inState) : _state(inState) { std::cout << "State based: " << inState << std::endl; } TestClass() : _state(1) { std::cout << "Default" << std::endl; } TestClass(const TestClass &inObject0) : _state(2) { std::cout << "Const Copy" << std::endl; } TestClass(TestClass &inObject0) : _state(3) { std::cout << "Copy" << std::endl; } TestClass(const TestClass &&inObject0) : _state(4) { std::cout << "Const Move" << std::endl; } TestClass(TestClass &&inObject0) : _state(5) { std::cout << "Move" << std::endl; } ~TestClass() { std::cout << "Destroy" << std::endl; } void call() { std::cout << "Call " << _state << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// int main() { TestClass theTestObject = TestClass(); theTestObject.call(); fflush(stdout); return 0; }
æåŸ ãããçµæïŒ
ããã©ã«ã
å®æ°ã³ããŒ
ã³ãŒã«1
ç Žå£ãã
å®éã®çµæïŒ
ããã©ã«ã
ã³ãŒã«1
ç Žå£ãã
ã€ãŸããã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã¯åŒã³åºãããŸããã§ããã ãããŠãã®ãšãã ããç§ã¯ããã«ããã¹ãããšãããŸããã Googleã§copy_elisionã«ã€ããŠèª¿ã¹ãŸãã ã
3.æãããçå®
äžèšã§èšãã°-C ++ã³ã³ãã€ã©ã¯ãèŠåããã©ã°ãªãã§ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã®åŒã³åºããç¡èŠãã代ããã«ãããšãã°ããªããžã§ã¯ãã®å®å šãªç¶æ ãçŽæ¥ã³ããŒã§ããŸãã åæã«ããã®ãããªã³ããŒã®ããã»ã¹ã§ã
æé©åã¯ãã¡ããåªããŠããŸã...ããããã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã§ããžãã¯ãå®è¡ããå¿ èŠãããå Žåã¯ã©ãã§ããããã ããšãã°ãã¹ããŒããã€ã³ã¿ãŒã®å Žå ãããŠãã³ããŒã³ã³ã¹ãã©ã¯ã¿ã®æ¬äœã«ããžãã¯ããªãå Žåã«-o1ã§ãã®ãããªæé©åãèš±å¯ããããšãäžå¯èœã ã£ãçç±ã¯ãç§ã«ã¯ãŸã äœãäžæã§ããïŒ..ä»ãŸã§ãããã¯ç§ã«ã¯æããã§ã¯ãããŸããã
4.決å®
ã¯ã©ã¹ãªããžã§ã¯ãã®æ§ç¯æã«ã³ã³ãã€ã©ã«ããžãã¯ãå®è¡ããã2ã€ã®æ¹æ³ãèŠã€ããŸããã
1ïŒã³ã³ãã€ã«ãã©ã°ãéããŠã æªãæ¹æ³ã ã³ã³ãã€ã©äŸåã ããšãã°ãg ++ã®å Žåã -fno-elide-constructorsãã©ã°ãèšå®ããå¿ èŠããããããã¯ãããžã§ã¯ãå šäœã«åœ±é¿ããïŒã²ã©ãïŒããé©åãªããã·ã¥/ãããã®å Žæã§ã³ã³ãã€ã©ãã©ã°èšå®ã䜿çšããå¿ èŠããããã³ãŒããä¹±éã«ãªããèªã¿ã«ãããªããŸãïŒç¹ã«ããã¯åã³ã³ãã€ã©ã§å®è¡ããå¿ èŠãããããšãèæ ®ããŠãã ããïŒã
2ïŒ æ瀺çãªããŒã¯ãŒããéããŠã ãããæªãæ¹æ³ã§ãããç§ã®æèŠã§ã¯ãããã¯ã³ã³ãã€ã«ãã©ã°ã䜿çšãããããåªããŠããŸãã
æ瀺çãªæå®åã¯ãåãã£ã¹ãæ§æã«ããã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã®æé»çãªäœæãçŠæ¢ããããã«å¿ èŠã§ãã ã€ãŸãã MyInt theMyInt = 1ã®ä»£ããã«MyInt theMyInt = MyIntïŒ1ïŒãèšè¿°ããå¿ èŠããããŸããã
ãã®åèªãã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã®åã«çœ®ããšãæé»çãªåå€æãããªããããããªããŸã-ãã®åãžã®ãã£ã¹ããçŠæ¢ãããŸãã
ãããã£ãŠãããšãã°ã次ã®
ã³ãŒã
#include <iostream> #include <stdio.h> class TestClass { private: int _state; public: TestClass(int inState) : _state(inState) { std::cout << "State based: " << inState << std::endl; } TestClass() : _state(1) { std::cout << "Default" << std::endl; } explicit TestClass(const TestClass &inObject0) : _state(2) { std::cout << "Const Copy" << std::endl; } } ~TestClass() { std::cout << "Destroy" << std::endl; } void call() { std::cout << "Call " << _state << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// int main() { TestClass theTestObject = TestClass(); theTestObject.call(); fflush(stdout); return 0; }
ç§ïŒg ++ 4.6.1ïŒã¯ãšã©ãŒãåŒãèµ·ãããŸããïŒ
ãšã©ãŒïŒ 'TestClass :: TestClassïŒTestClassïŒ'ã®åŒã³åºãã«äžèŽããé¢æ°ããããŸãã
ããã«é¢çœãã®ã¯ã次ã®ãããªC ++æ§æã®ããã§ãïŒTestClass theTestObjectïŒTestClassïŒïŒïŒãæ©èœããŸãããé¢æ°ãã€ã³ã¿ãŒã®å®£èšãšèŠãªããããšã©ãŒãçºçããããã§ãã
ãšã©ãŒïŒ 'theTestObject'ã®ã¡ã³ã㌠'call'ã®èŠæ±ãããã¯éã¯ã©ã¹å 'TestClassïŒTestClassïŒ*ïŒïŒïŒïŒ'ã§ãã
ãããã£ãŠãã³ã³ãã€ã©ã«åŒ·å¶çã«ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒãå®è¡ããã代ããã«ããã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®åŒã³åºããçŠæ¢ããŸãã
幞ããªããšã«ããã®ãããªè§£æ±ºçãæãã€ããŸããã å®éãã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒãç¡å¹ã«ããããšã§ãã³ã³ãã€ã©ãŒã«ãçŸåšã®ã¯ã©ã¹ãšåããã³ãã¬ãŒãåŒæ°ãæã€ãã³ãã¬ãŒãã³ã³ã¹ãã©ã¯ã¿ãŒä»æ§ã䜿çšããããã«åŒ·å¶ããŸããã ã€ãŸããããªããžã§ã¯ãããã®åã«ãã£ã¹ããããã®ã§ã¯ãªãããåããã³ãã¬ãŒãåŒæ°ãæã€åã«ãã£ã¹ããããã®ã§ãå¥ã®ã¡ãœãããçæããŸãããã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã眮ãæããŸãã
ãããèµ·ãã£ã
template<typename T_Type, typename T_Holding, typename T_Access> class DReference { . . . IDSharedMemoryHolder *_holder; void retain(IDSharedMemoryHolder *inHolder) { . . . } void release() { . . . } . . . ~DReference() { release(); } //NB: Workaround for Copy elision explicit DReference( const OwnType &inLReference) : _holder(NULL), _holding(), _access() { // Call for some magic cases retain(inLReference._holder); } template<typename T_OtherType, typename T_OtherHolding, typename T_OtherAccess> DReference( const DReference<T_OtherType, T_OtherHolding, T_OtherAccess> & inLReference) : _holder(NULL), _holding(), _access() { retain(inLReference._holder); } . . . }
ãã¹ãäŸã§ã¯ããã®æŸèæã®é¡äŒŒç©ã¯æ¬¡ã®ããã«ãªããŸãã
ãã¹ãã³ãŒããšç°¡åãªèª¬æ
åããããã ãã³ãã¬ãŒãã®ä»æ§ãã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã®çœ®ãæã...ééã£ããã³ãã¬ãŒãã䜿çšãããããããã¯æªã解決çã§ããããšãããããŸãã 誰ããæé«ã®æ¹æ³ãç¥ã£ãŠããå Žå-éäŒããŸãã
#include <iostream> #include <stdio.h> class TestClass { private: int _state; public: TestClass(int inState) : _state(inState) { std::cout << "State based: " << inState << std::endl; } TestClass() : _state(1) { std::cout << "Default" << std::endl; } explicit TestClass(const TestClass &inObject0) : _state(2) { std::cout << "Const Copy" << std::endl; } template<typename T> TestClass(const T &inObject0) : _state(13) { std::cout << "Template Copy" << std::endl; } ~TestClass() { std::cout << "Destroy" << std::endl; } void call() { std::cout << "Call " << _state << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// int main() { TestClass theTestObject = TestClass(); theTestObject.call(); fflush(stdout); return 0; }
åããããã ãã³ãã¬ãŒãã®ä»æ§ãã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã®çœ®ãæã...ééã£ããã³ãã¬ãŒãã䜿çšãããããããã¯æªã解決çã§ããããšãããããŸãã 誰ããæé«ã®æ¹æ³ãç¥ã£ãŠããå Žå-éäŒããŸãã
çµè«ã®ä»£ããã«
C ++ãšabout-C ++ã3幎é䜿çšããŠããæ°äººã®å人ã«ã³ããŒã®çç¥ã«ã€ããŠè©±ãããšãã圌ã
ãã®èšäºã誰ãã«ãšã£ãŠæçšã§ããã誰ãã®æéãç¯çŽããããšãé¡ã£ãŠããŸãã
PSïŒæ°ã¥ããèŠèœãšãã«ã€ããŠæžããŠãã ãã-ç·šéããŸãã
ç·šéè ïŒ
ã³ã¡ã³ãã¯æ£ããã§ããç§ã¯äžè¬çã«åé¡ã誀解ããŸããã èšäºã®è«çãšã©ãŒãç¹å®ããŠããããç¹ã«Monnorochã«æè¬ããŸãã
次ã®ãã¹ãã³ãŒããèšè¿°ããããšã§ãæ£ããåºåãåŸãããŸããã
ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒãšåºåãåãããã³ãã¬ãŒãã¯ã©ã¹
åºåïŒ
/////////////////////////////////////////////////////////////////////////////// #include <iostream> /////////////////////////////////////////////////////////////////////////////// template<typename T_Type> class TestTemplateClass { private: typedef TestTemplateClass<T_Type> OwnType; T_Type _state; public: TestTemplateClass() : _state() { std::cout << "Default constructor" << std::endl; } TestTemplateClass(int inState) : _state(inState) { std::cout << "State constructor" << std::endl; } TestTemplateClass(const OwnType &inValue) { std::cout << "Copy constructor" << std::endl; } template<typename T_OtherType> TestTemplateClass(const TestTemplateClass<T_OtherType> &inValue) { std::cout << "Template-copy constructor" << std::endl; } template<typename T_OtherType> void operator = (const TestTemplateClass<T_OtherType> &inValue) { std::cout << "Operator" << std::endl; } ~TestTemplateClass() { std::cout << "Destructor" << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// TestTemplateClass<int> createFunction() { return TestTemplateClass<int>(); } /////////////////////////////////////////////////////////////////////////////// int main() { TestTemplateClass<int> theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; } ///////////////////////////////////////////////////////////////////////////////
åºåïŒ
Default constructor Copy constructor Destructor Copy constructor Destructor Finished Destructor
ã€ãŸããåé¡ã¯ã³ããŒã®çç¥ã§ã¯ãªããããã¯ã¯å¿ èŠãããŸããã§ããã
æ¬åœã®ééãã¯åœããåã®ããšã§ããã ä»ãç§ã¯ãã¹ãŠããã§ãã¯ããããšãªãèšäºãæžãããšãçŽæããããšãæ¥ããŠããŸãã
å®éãã¹ããŒããã€ã³ã¿ãŒã¯3ã€ã®ãã³ãã¬ãŒãåŒæ°ãåãå ¥ããŸãã
template<typename T_Type, typename T_Holding, typename T_Access> class DReference { . . .
- T_Typeã¯ãã¹ããŒããã€ã³ã¿ãŒã·ã¹ãã ã«ãã£ãŠå¶åŸ¡ããããªããžã§ã¯ãã¿ã€ãã§ãã
- T_Holding-ã¡ã¢ãªæææš©æŠç¥ã
- T_Accessã¯ã¡ã¢ãªã¢ã¯ã»ã¹æŠç¥ã§ãã
ãã®ã¹ããŒããã€ã³ã¿ãŒã®å®è£ ã«ãããåäœãæè»ã«ã«ã¹ã¿ãã€ãºã§ããŸãããåæã«äœ¿çšãé¢åã«ãªããŸãïŒç¹ã«æŠç¥ã¯ãã³ãã¬ãŒãã¯ã©ã¹ã§ããããïŒã
匷åãªãã€ã³ã¿ãŒã宣èšããäŸïŒ
DReference<MyType, DReferenceStrongHolding<MyType>, DReferenceCachingAccess< MyType > > theReference;
ã³ãŒããä¹±éã«ãªãã®ãé¿ããããã«ãC ++ 11æšæºã®æ©èœ-template-aliasã䜿çšããããšèããŸããã ããããå€æããããã«ãg ++ 4.6.1 ã¯ãããããµããŒãããŠããŸãã ã ãã¡ãããç°å¢ãã»ããã¢ããããããšããã¡ããã¡ãã«ããŠããŒã ããããããžã§ã¯ããæžããšããããªãã¯é¢åãããã®ã§ãå¥ã®åé¿çãè¡ããç¶æ¿ã䜿çšããŠåŒæ°ãåãé€ãããšã«ããŸããã
template<typename T_Type> class DStrongReference : public DReference< T_Type, DReferenceStrongHolding<MyType>, DReferenceCachingAccess< MyType > > { . . .
ãã®å Žåãã³ã³ã¹ãã©ã¯ã¿ãŒãç¶æ¿ãããªãããã DStrongReferenceã®äžé£ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãå®çŸ©ããŠã DReferenceåºæ¬ã¯ã©ã¹ã®å¯Ÿå¿ããã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãå¿ èŠããããŸããã ãããŠããã¡ãããç§ã¯ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒãèŠéããŸãã...äžè¬ã«ãããããã¹ãŠã®åéºã®åŸã«ç§ãäžããããšãã§ããå¯äžã®ã¢ããã€ã¹ã¯ãç§ãå ¥ã£ããã®ãããªæããªç¶æ³ã«å ¥ããªãããã«ãã³ãã¬ãŒãã䜿çšãããšããéåžžã«æ³šæããããšã§ãã
PSïŒããã¯ããã³ãã¬ãŒããšã€ãªã¢ã¹ã眮ãæããç¶æ¿ã䜿çšãããã¹ãã§ãïŒ ToSHiCã«ãããåºåã«æž¡ãè¯ãã¢ããã€ã¹ãããããšãïŒã
ãã¹ãã·ãã¥ã¬ãŒã·ã§ã³ãã³ãã¬ãŒããšã€ãªã¢ã¹
åºåïŒ
/////////////////////////////////////////////////////////////////////////////// #include <iostream> /////////////////////////////////////////////////////////////////////////////// template<typename T_Type, typename T_Strategy> class TestTemplateClass { private: typedef TestTemplateClass<T_Type, T_Strategy> OwnType; T_Type _state; T_Strategy _strategy; public: TestTemplateClass() : _state(), _strategy() { std::cout << "Default constructor: " << this << std::endl; } TestTemplateClass(int inState) : _state(inState), _strategy() { std::cout << "State constructor: " << this << std::endl; } TestTemplateClass(const OwnType &inValue) : _state(), _strategy() { std::cout << "Copy constructor: " << this << " from " << &inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClass( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : _state(), _strategy() { std::cout << "Template-copy constructor: " << this << std::endl; } void operator = (const OwnType &inValue) { std::cout << "Assigning: " << this << " from " << inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { std::cout << "Assigning: " << this << " from " << &inValue << std::endl; } ~TestTemplateClass() { std::cout << "Destructor: " << this << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// template<typename T_Type> class TestTemplateClassIntStrategy : public TestTemplateClass<T_Type, int> { private: //- Types typedef TestTemplateClassIntStrategy<T_Type> OwnType; typedef TestTemplateClass<T_Type, int> ParentType; public: TestTemplateClassIntStrategy() : ParentType() { } TestTemplateClassIntStrategy(int inState) : ParentType(inState) { } TestTemplateClassIntStrategy(const OwnType &inValue) : ParentType(inValue) { } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClassIntStrategy( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : ParentType(inValue) { } //- Operators void operator = (const OwnType &inValue) { ParentType::operator =(inValue); } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { ParentType::operator =(inValue); } }; /////////////////////////////////////////////////////////////////////////////// TestTemplateClassIntStrategy<int> createFunction() { return TestTemplateClassIntStrategy<int>(); } int main() { TestTemplateClassIntStrategy<int> theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; }
åºåïŒ
Default constructor: 0x28fed8 Copy constructor: 0x28ff08 from 0x28fed8 Destructor: 0x28fed8 Copy constructor: 0x28ff00 from 0x28ff08 Destructor: 0x28ff08 Finished Destructor: 0x28ff00
å²ãåœãŠã¹ããŒãã¡ã³ãã®åŒã³åºã
. . . int main() { TestTemplateClassIntStrategy<int> theReference; theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; }
Default constructor: 0x28ff00 Default constructor: 0x28fed8 Copy constructor: 0x28ff08 from 0x28fed8 Destructor: 0x28fed8 Assigning: 0x28ff00 from 0x28ff08 Destructor: 0x28ff08 Finished Destructor: 0x28ff00
ãã®æ¹æ³ã®éèŠãªãã€ãã¹ç¹ïŒãã®æ¹æ³ã§ã¹ããã³ã°ãã€ã³ã¿ãŒãšãŠã£ãŒã¯ãã€ã³ã¿ãŒãå®çŸ©ãããšããããã¯å®å šã«ç°ãªãã¿ã€ãã«ãªãïŒ1ã€ã®ãã³ãã¬ãŒãã¯ã©ã¹ã«é¢é£ä»ããããªãïŒãåæåæã«ããããçŽæ¥å²ãåœãŠãããšã¯ã§ããŸããã
<ç·šéçªå·2>
åã³æ¥ãã§äœãã確èªããŸããã ç§ã¯å€ã«ãããåŸãã çµå±ã®ãšãã...ãããã®ã¯ã©ã¹ã«ã¯ãå ±éã®ãã³ãã¬ãŒãç¥å ã1ã€ãããŸãã
ã€ãŸããä»»æã®DReferenceã®ãã³ãã¬ãŒãã³ã³ã¹ãã©ã¯ã¿ãŒãåŸç¶ïŒãã³ãã¬ãŒããšã€ãªã¢ã¹ãæš¡å£ããïŒã§èšè¿°ãããŠããå Žåã次ã®ã³ãŒãã§ã¯ãã¹ãŠåé¡ãããŸããã
ã³ãŒã
DStrongReference<Type> theStrongReference; // . : // // template<typename Type, typename Owning, typename Holding> // DWeakReference::DWeakReference(const DReference<Type, Owning, Holding> &ref) : Parent(ref) { } // // , DStrongReference DReference. // DWeakReference<Type> theWeakReference = theStrongReference;
ãã®ããã«æ§æããã2ã€ã®ã¯ã©ã¹ã®ãã¹ãã³ãŒãïŒ
ç¶æ¿ã«ãããã³ãã¬ãŒããšã€ãªã¢ã¹ïŒ2ã€ã®æ¬äŒŒãšã€ãªã¢ã¹
åºåïŒ
//============================================================================ // Name : demiurg_application_example.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ /////////////////////////////////////////////////////////////////////////////// #include <iostream> /////////////////////////////////////////////////////////////////////////////// template<typename T_Type, typename T_Strategy> class TestTemplateClass { private: typedef TestTemplateClass<T_Type, T_Strategy> OwnType; T_Type _state; T_Strategy _strategy; public: TestTemplateClass() : _state(), _strategy() { std::cout << "Default constructor: " << this << std::endl; } TestTemplateClass(int inState) : _state(inState), _strategy() { std::cout << "State constructor: " << this << std::endl; } TestTemplateClass(const OwnType &inValue) : _state(), _strategy() { std::cout << "Copy constructor: " << this << " from " << &inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClass( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : _state(), _strategy() { std::cout << "Template-copy constructor: " << this << std::endl; } void operator = (const OwnType &inValue) { std::cout << "Assigning: " << this << " from " << &inValue << std::endl; } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { std::cout << "Assigning: " << this << " from " << &inValue << std::endl; } ~TestTemplateClass() { std::cout << "Destructor: " << this << std::endl; } }; /////////////////////////////////////////////////////////////////////////////// //- Integer strategy template<typename T_Type> class TestTemplateClassIntStrategy : public TestTemplateClass<T_Type, int> { private: //- Types typedef TestTemplateClassIntStrategy<T_Type> OwnType; typedef TestTemplateClass<T_Type, int> ParentType; public: TestTemplateClassIntStrategy() : ParentType() { } TestTemplateClassIntStrategy(int inState) : ParentType(inState) { } TestTemplateClassIntStrategy(const OwnType &inValue) : ParentType(inValue) { } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClassIntStrategy( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : ParentType(inValue) { } //- Operators void operator = (const OwnType &inValue) { ParentType::operator =(inValue); } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { ParentType::operator =(inValue); } }; //- Boolean strategy template<typename T_Type> class TestTemplateClassBoolStrategy : public TestTemplateClass<T_Type, bool> { private: //- Types typedef TestTemplateClassBoolStrategy<T_Type> OwnType; typedef TestTemplateClass<T_Type, bool> ParentType; public: TestTemplateClassBoolStrategy() : ParentType() { } TestTemplateClassBoolStrategy(int inState) : ParentType(inState) { } TestTemplateClassBoolStrategy(const OwnType &inValue) : ParentType(inValue) { } template<typename T_OtherType, typename T_OtherStrategy> TestTemplateClassBoolStrategy( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) : ParentType(inValue) { } //- Operators void operator = (const OwnType &inValue) { ParentType::operator =(inValue); } template<typename T_OtherType, typename T_OtherStrategy> void operator = ( const TestTemplateClass<T_OtherType, T_OtherStrategy> &inValue) { ParentType::operator =(inValue); } }; /////////////////////////////////////////////////////////////////////////////// TestTemplateClassBoolStrategy<int> createFunction() { return TestTemplateClassBoolStrategy<int>(); } int main() { TestTemplateClassIntStrategy<int> theReference; theReference = createFunction(); std::cout << "Finished" << std::endl; return 0; }
åºåïŒ
Default constructor: 0x28fed8 Copy constructor: 0x28ff08 from 0x28fed8 Destructor: 0x28fed8 Copy constructor: 0x28ff00 from 0x28ff08 Destructor: 0x28ff08 Finished Destructor: 0x28ff00
äžè¬ã«ããã¹ãŠãæ©èœããŸã
</ç·šéçªå·2>
1eqinfinityã®ãããã§ãã¹ãã«ãšã©ãŒãææããŠãããTorvald3d ã