| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Krivenok Dmitry" |
| Date: |
24 Mar 2006 06:07:20 AM |
| Object: |
Question about Loki::Singleton |
Hello All!
This is example of code:
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef A_H_
#define A_H_
#include <Loki/Singleton.h>
#include <Loki/SmartPtr.h>
#include <iostream>
class A
{
public:
A() { std::cout << "A constructor" << std::endl;}
~A() { std::cout << "A destructor" << std::endl;}
};
typedef Loki::SingletonHolder<
Loki::SmartPtr<A>,
Loki::CreateUsingNew,
Loki::NoDestroy,
Loki::SingleThreaded
APtrSingleton_t;
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////
I can use APtrSingleton_t::Instance() in my program, but it's very
uneasily.
How to define (short) alternative name of APtrSingleton_t::Instance()?
Thanks!
.
|
|
| User: "Ben Pope" |
|
| Title: Re: Question about Loki::Singleton |
24 Mar 2006 06:53:05 AM |
|
|
Krivenok Dmitry wrote:
Hello All!
This is example of code:
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef A_H_
#define A_H_
#include <Loki/Singleton.h>
#include <Loki/SmartPtr.h>
#include <iostream>
class A
{
public:
A() { std::cout << "A constructor" << std::endl;}
~A() { std::cout << "A destructor" << std::endl;}
};
typedef Loki::SingletonHolder<
Loki::SmartPtr<A>,
Loki::CreateUsingNew,
Loki::NoDestroy,
Loki::SingleThreaded
APtrSingleton_t;
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////
I can use APtrSingleton_t::Instance() in my program, but it's very
uneasily.
How to define (short) alternative name of APtrSingleton_t::Instance()?
Thanks!
A* x() {
return APtrSingleton_t::Instance();
}
Then whenever you want the instance, just do:
A* a = x();
Not very useful if you ask me, though.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
.
|
|
|
|
| User: "mlimber" |
|
| Title: Re: Question about Loki::Singleton |
24 Mar 2006 07:01:11 AM |
|
|
Krivenok Dmitry wrote:
Hello All!
This is example of code:
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef A_H_
#define A_H_
#include <Loki/Singleton.h>
#include <Loki/SmartPtr.h>
#include <iostream>
class A
{
public:
A() { std::cout << "A constructor" << std::endl;}
~A() { std::cout << "A destructor" << std::endl;}
};
typedef Loki::SingletonHolder<
Loki::SmartPtr<A>,
Loki::CreateUsingNew,
Loki::NoDestroy,
Loki::SingleThreaded
APtrSingleton_t;
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////
I can use APtrSingleton_t::Instance() in my program, but it's very
uneasily.
How to define (short) alternative name of APtrSingleton_t::Instance()?
Thanks!
You could create a macro (yuck!), an inline function, or a local
reference (my preference):
Loki::SmartPtr<A>& ptr = APtrSingleton_t::Instance();
Cheers! --M
.
|
|
|
|
| User: "Krivenok Dmitry" |
|
| Title: Re: Question about Loki::Singleton |
24 Mar 2006 10:00:10 AM |
|
|
Of course s/Loki::SmartPtr<A>/A*/g :))
.
|
|
|
|

|
Related Articles |
|
|