Question about Loki::Singleton

K

Krivenok Dmitry

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!
 
B

Ben Pope

Krivenok said:
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
#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
 
M

mlimber

Krivenok said:
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
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top