acl/lib_acl_cpp/samples/singleton/singleton.cpp

97 lines
2.3 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
// singleton.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
//
#include "stdafx.h"
#include "acl_cpp/stdlib/singleton.hpp"
// <20><><EFBFBD>ٵ<EFBFBD><D9B5>ù<EFBFBD><C3B9>̵ļ<CCB5><C4BC><EFBFBD><EFBFBD><EFBFBD>
static int __nstep = 0;
class singleton_test : public acl::singleton <singleton_test>
{
public:
singleton_test()
{
// <20><><EFBFBD><EFBFBD><EFBFBD>þ仰<C3BE><E4BBB0>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD> main <20><><EFBFBD><EFBFBD><EFBFBD>еĴ<D0B5>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// ˵<><CBB5><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> main <20><><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
printf("step %d: singleton_test construct called\r\n", ++__nstep);
fflush(stdout);
}
virtual ~singleton_test()
{
printf("step %d: singleton_test destruct called\r\n", ++__nstep);
fflush(stdout);
getchar();
}
const singleton_test& init() const
{
printf("step %d: singleton_test init called\r\n", ++__nstep);
fflush(stdout);
return *this;
}
const singleton_test& set(const char* name) const
{
printf("step %d: singleton_test set(%s) called\r\n", ++__nstep, name);
fflush(stdout);
return *this;
}
};
//////////////////////////////////////////////////////////////////////////
class singleton_test2
{
public:
singleton_test2()
{
// <20><><EFBFBD><EFBFBD><EFBFBD>þ仰<C3BE><E4BBB0>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD> main <20><><EFBFBD><EFBFBD><EFBFBD>еĴ<D0B5>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// ˵<><CBB5><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> main <20><><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
printf("step %d: singleton_test2 construct called\r\n", ++__nstep);
fflush(stdout);
}
virtual ~singleton_test2()
{
printf("step %d: singleton_test2 destruct called\r\n", ++__nstep);
fflush(stdout);
getchar();
}
const singleton_test2& init() const
{
printf("step %d: singleton_test2 init called\r\n", ++__nstep);
fflush(stdout);
return *this;
}
const singleton_test2& set(const char* name) const
{
printf("step %d: singleton_test2 set(%s) called\r\n", ++__nstep, name);
fflush(stdout);
return *this;
}
};
//singleton_test::get_const_instance();
int main()
{
printf("step %d: first line in main\r\n", ++__nstep);
fflush(stdout);
// <20><><EFBFBD><EFBFBD>һ
const singleton_test& test1 = singleton_test::get_instance().init();
const singleton_test& test2 = singleton_test::get_instance();
test1.set("test1");
test2.set("test2");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> VC2003 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> release <20>汾ʱ<E6B1BE><CAB1>
// <20>÷<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>Ա<EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD> main ֮ǰ<D6AE><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
acl::singleton2<singleton_test2>::get_instance().init();
acl::singleton2<singleton_test2>::get_instance().set("test1");
acl::singleton2<singleton_test2>::get_instance().set("test2");
return 0;
}