Open fafner2000 opened 6 months ago
No news. Is it a bug, or just me messing up ?
Just asking so I know if I have to fix my code :-)
static member is initialized staticaly not by constructor. I am not expert to C++ but I think that constructor must be public. I don't know what you want to achive. May be @pchapin will comment.
The class is supposed to modelize a singleton, that is a class with a single instance. The single instance is modelized by a public static field, which instantiates the class with a constructor (as any standard initializer). To avoid the class being instantiated a second time, all constructors are private, and because the static field belongs to the class, I expect it to have access to private constructors.
But the class is not static (single instance) that you cannot assume it untill all members will be static. still it can have multiple instances (with shared static members) and constructor/destructor has sense. I think static class can have only automatic constructor/destructor. But I am not familiare with new C++ standards that I can be wrong.
Take into account that OW supports C++98 standard only.
Hello,
I have an access error when trying to initialize a static class field with a private constructor of the same class (classical singleton pattern). I had the exact same problem at the exact same location when compiling with Visual Studio 6. I am not exactly using that compiler every day, so I never really cared about it. Now I have the same error with Watcom, which makes me wonder if I may be trespassing on some specification I don't know about. However, I made some searches and ended up only on (very old) posts from people having the problem with Visual Studio 6.
So here is the code:
Constructors and destructor are private to avoid "accidents". Changing their access to public solves the problem, but exposes to possible buggy code that would try to create or destroy what is supposed to be a singleton. I also tried to add the static keyword on front of the line, with no change. As it is, it compiles fine with all compilers I know (gcc, clang, RECENT versions of Visual Studio, etc; all spanning many versions).
However, Watcom gives me this error: test.cpp(14): Error! E148: col(34) access to private member 'Singleton::Singleton' is not allowed
Additionally, it looks like I can somewhat get away with it by not using the default constructor, and writing: const Singleton Singleton::UNIQUE=1; const Singleton Singleton::UNIQUE(1); // same behavior with this variant
Giving this error: test.cpp(14): Error! E148: col(36) access to private member 'Singleton::~Singleton' is not allowed
This time the (non-default) constructor seems to be accessible, but not the destructor.