On Friday 03 June 2005 15:09, Mws wrote: > On Friday 03 June 2005 15:45, Filipe Sousa wrote: > > On Friday 03 June 2005 14:22, Mws wrote: > > > template<class IMPLEMENTINGCLASS> > > > class TSingleton > > > { > > > private: > > > static IMPLEMENTINGCLASS* instance; > > > > > > public: > > > static IMPLEMENTINGCLASS* getInstance() > > > { > > > //debugging purposes > > > std::cout << "before" << instance << std::endl; > > > if(!instance) > > > { > > > instance = new IMPLEMENTINGCLASS(); > > > } > > > std::cout << "after" << instance << std::endl; > > > return instance; > > > } > > > > > > void dispose() > > > { > > > if(instance) > > > { > > > delete instance; > > > instance = NULL; > > > } > > > } > > > }; > > > > > > template<class IMPLEMENTINGCLASS> IMPLEMENTINGCLASS* > > > TSingleton<IMPLEMENTINGCLASS>::instance = NULL; > > > > But I still have more then one instance: > > > > before0 > > my_singleton::my_singleton() > > after0x804a008 > > before0 > > my_singleton::my_singleton() > > after0x804a018 > > what? > show me your main please > > regards > mws -- Filipe Sousa
#include "singleton.h" int main() { foo(); my_singleton::getInstance()->foo(); return 0; }
#pragma once #include <iostream> #define EXPORT __attribute__ ((visibility("default"))) template<class IMPLEMENTINGCLASS> class TSingleton { private: static IMPLEMENTINGCLASS* instance; public: static IMPLEMENTINGCLASS* getInstance() { //debugging purposes std::cout << "before" << instance << std::endl; if(!instance) { instance = new IMPLEMENTINGCLASS(); } std::cout << "after" << instance << std::endl; return instance; } void dispose() { if(instance) { delete instance; instance = NULL; } } }; template<class IMPLEMENTINGCLASS> IMPLEMENTINGCLASS* TSingleton<IMPLEMENTINGCLASS>::instance = NULL; class EXPORT my_singleton : public TSingleton<my_singleton> { public: my_singleton() { std::cout << __PRETTY_FUNCTION__ << std::endl; } void foo() {} }; EXPORT void foo();
#include "singleton.h" void foo() { my_singleton::getInstance()->foo(); }
all: c++ -g -fPIC -Wall -fvisibility-inlines-hidden -fvisibility=hidden -c singleton.cc -o singleton.o c++ -fPIC -fvisibility-inlines-hidden -fvisibility=hidden -shared -Wl,-soname,libsingleton.so -o libsingleton.so singleton.o c++ -Wall -g -c main.cc -o main.o c++ -g -fPIC main.o -o main -rdynamic -L${PWD} -lsingleton -Wl,-rpath,${PWD} clean: rm -f *.o *.so main
Attachment:
pgpzt8yGqjJmW.pgp
Description: PGP signature