-1

Using the Singleton patterns from:

C++ Singleton design pattern and http://www.yolinux.com/TUTORIALS/C++Singleton.html

I have made a specialized Singleton class rather than using one of the hidden ones e.g. Boost::Serialization::Singleton.

How Can I alias the singleton call though? It's quite irksome to have to write

namespace::class::get_instance().method() everywhere

specifically how can i alias the namespace::class::get_instance() part of it?

Specifically I would like to be able to do this on the stack rather than the heap since the public constructor, copy constructor and assignment operator are all privatized ... but don't seem to be able to.

Using a function pointer didn't work for me.

edit: using no macros as those are in general frowned upon as far as i know. / I know of the typedef, I was hoping for something simple relative like in other languages like

Singleton.method() Also i've seen very few cases / answers on Stackoverflow that should good reasons to use a #define

This should be calling a library class from a main/usage perspective.

slightly relevant: How can I make an alias to a singleton function? but I don't need only a specific function.

idealized example:

defined library LIB.

user:

LIB::server_interface::get_instance()::do_something().

say said lib does automatic connection pooling / other operations. and you're interfacing extensively with some service, having to prepend the following extensively is a pain and does not make for pretty code. Furthermore as a user using the lib, you don't want to be putting namespace{

} in your code I would think.

Community
  • 1
  • 1
Eiyrioü von Kauyf
  • 4,481
  • 7
  • 32
  • 41

3 Answers3

4

Since a reference is an alias to an object, perhaps what you need is to bind a reference to the instance?

namespace::class& sngl_ref = namespace::class::get_instance();
....
sngl_ref.method();
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

I wonder what you think the singleton pattern is for. It has a very limited usage where some object must exist at most once within a program. For example, a program that manipulates a display essentially needs a single display object. It would be am error in such a program to create a duplicate. Singletons are, at least IMO, about data. If your program needs a single data object in a context where it would be an error to duplicate that data then you have a candidate for a singleton. I am lost by that get_instance() function. There should only be a single instance even if it goes under many names.

For example, my books include a specialist graphics window object (note that the books are for novices) of a type 'playpen'. The user is free to declare playpen 'objects'. copy them etc. to their hearts content but always the program will contain at most a single instance of the data. If no playpen object currently exists in a program declaring one will create the necessary underlying data structure. If a playpen object already exists, declaring a 'new' one just provides a new name (and way to manipulate) for the already existing one.

The fact that you are asking your question suggests very strongly that you are suffering from 'shiny new hammer' syndrome and are trying to bash screws with it.

Francis

0

Simplest way is to declare a typedef shortcut:

typedef namespace::class nc;

This would reduce the typing significantly already.

Another way is do have unit local method declarations in your client compilation unit:

namespace
{
    void call_method()
    {
        namespace::class::get_instance().method();
    }

    // Alternatively to shortcut the getInstance() part specifically
    namespace::class& getClassInstance()
    {
        return namespace::class::get_instance();
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190