2

While ago I read about not allowing defining further instances of one object. But I cannot find that article again. Could anybody tell me how to prevent defining any other object from class CLog?

class CLog
{
........


} log;
stefan
  • 10,215
  • 4
  • 49
  • 90
barej
  • 1,330
  • 3
  • 25
  • 56
  • 7
    Google *Singleton pattern* and then read about why you [shouldn't be using them](http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/). – Praetorian Feb 03 '15 at 08:53
  • @Praetorian I want to prevent defining log2 and log3 ... . Even if my program is in debug mod, do you believe I should avoid it? – barej Feb 03 '15 at 08:55
  • Debug mode has nothing to do with your definings. Yes, Singleton is your answer %) – Alexey Andronov Feb 03 '15 at 08:56
  • So, as you say, there is no directly supported way by C++? – barej Feb 03 '15 at 08:57
  • 3
    @barej Here is what you are [looking for](http://stackoverflow.com/a/1008289/4371276). As for using or not using the singleton pattern, anyone who states that singleton is inherently bad is as dumb as anyone stating that singleton is the answer to everything. – dmg Feb 03 '15 at 09:00
  • 4
    @barej So don't create more than one instance then. Here's a [question](http://stackoverflow.com/q/8337300/241631) that discusses implementing logging without use of a singleton. – Praetorian Feb 03 '15 at 09:00
  • Thank you @Praetorian . you made my work much easier too. – barej Feb 03 '15 at 09:10
  • Please do not put a "solved" "tag" to your question's title. Accept an answer instead or write a new one describing what you ultimately chose. – stefan Feb 03 '15 at 09:53
  • @stefan, The answer is given in comments by Praetorian – barej Feb 03 '15 at 10:16
  • @barej I don't care _where exactly_ the answer is. But there's one place, the answer shouldn't be: In the question. – stefan Feb 03 '15 at 10:19

2 Answers2

0

An alternative solution to a singleton is to use the Service Locator pattern.

A good description of it and how to use it is described in http://gameprogrammingpatterns.com/service-locator.html.

Again like the singleton this pattern should be used sparingly however a logging system can be a good use case for it and if nothing else it teaches you some new design patterns.

const_ref
  • 4,016
  • 3
  • 23
  • 38
-1

Make your constructor as private. Define one public method which returns instance of class.

class sample{
 private:
    sample(){};
    static *sample instance;
public:
static *sample getInstance(){
   if (instance != null)
      instance = new sample();
   return instance;
}

getInstance() method creates class instance before creating it checks whether the instance exists, if it exists it retuns the existing one otherwise creates new one.In this way you can make your class to create single instance of class. As constructor is private no one can create objects using constructor.

user3863488
  • 227
  • 1
  • 13
  • This is not thread-safe; make the instance itself a local static. – rubenvb Feb 03 '15 at 09:55
  • in essence athough of course the class is not thread safe and the * is in the wrong placev and you haven't disabled copy-construction – CashCow Feb 03 '15 at 09:55
  • @rubenv that is the Meyers singleton. the C++11 standard declares your construct is indeed threadsafe but destruction order is undetermined. call_once is a good way to go too – CashCow Feb 03 '15 at 09:56