-1

I wrote an singletone class as below:

class logger
{
    FILE *__fp_log = NULL;
    const char* __logpath;
    //Constructor
    logger();
    static logger* __instance;
    logger& operator=(const logger &) ;
    logger(const logger &);
public:
    ~logger();
    void write_file( const char* ,... );
    static logger* getInstance();
};

I understand that I need to make Copy Constructor and assignment operator as private (reference here). But still I can access them from main():

logger* log = logger::getInstance();
logger* log2 = logger::getInstance();

log = logger::getInstance();

I should get a compilation warning, any pointer why compilation is not complaining? I am compiling using g++:

g++ .\main.cpp .\logger.cpp
  • 6
    There is no copying happening there, all you do is assign pointers – UnholySheep Mar 15 '22 at 18:53
  • 1
    I'm not seeing any copying happening here. `log` is a `logger*`, not a `logger`. – NathanOliver Mar 15 '22 at 18:55
  • 1
    Identifiers containing double underscores are reserved for the C++ implementation. You are not allowed to declare them. – user17732522 Mar 15 '22 at 18:55
  • If you don't need access to the copy constructor/assignment operator, delete them to be sure that regardless of the access the compiler knows the function must never be used. Copying pointers does not have anything to do with the copy constructor/assignment operator. The assignment operator for pointers is a builtin operator that cannot be overloaded... Consider using a "magic static" btw, see https://stackoverflow.com/a/11711991/2991525 – fabian Mar 15 '22 at 18:56
  • In the linked answer _references_ are used, not pointers. Assignment between the references is assignment directly between the referenced objects. Assignment between pointers is just assignment of pointer values. – user17732522 Mar 15 '22 at 18:57
  • Side note: [a more detailed discussion of the magic static brought up by fabian](https://stackoverflow.com/a/1008289/4581301) – user4581301 Mar 15 '22 at 19:04
  • The better [_Singleton pattern approach_](https://stackoverflow.com/questions/1008019/c-singleton-design-pattern) BTW. – πάντα ῥεῖ Mar 15 '22 at 19:07

1 Answers1

0

You're not performing any copying or assignment of loggers here. You're copying/assigning pointers to logger, which doesn't involve the logger object being pointed to at all.

The operation you're trying to prohibit would be something like:

 logger log = *logger::getInstance();  // Dereferencing and copying to separate log object

which would try to make an unrelated instance that is a copy of the singleton instance.

Analogy: Your house is unique. No one can make an exact copy of your house (at least, it's unreasonably difficult to do so) without access to the blueprints (the constructor). But someone who has the address of your house can easily copy the address to a postcard, without affecting your house in any way. Your getInstance() method is returning a copy of the address, which you can "write" to a "postcard" (a local pointer to logger). It does not affect the "house" (the singleton logger instance) in any way. Any such copy of the address can be used to find the "house"/logger instance, but by making the constructors and assignment operator private, you've denied access to the house's blueprints (constructor/assignment), so they're limited to walking around your "house" (calling methods on the singleton logger), they can't just make a duplicate of it.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271