Archived

This topic is now archived and is closed to further replies.

Meyers Singleton won't compile with VC++

This topic is 5622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone tell me why the fuck a Meyers singleton won''t compile in VC++ but is fine with Borland Builder. Below is a simple example of a meyers singleton that won''t compile. class MyClass { MyClass() {} ~MyClass() {} // undefined MyClass( const MyClass & ); MyClass& operator=( MyClass ); public: static MyClass& Get_Instance() { static MyClass instance; return instance; } }; However if I make the destructor public, it compiles fine but that shouldn''t have to be. Any help will be much appreciated.

Share this post


Link to post
Share on other sites
Someone posted about this issue a while back (Stoffel?). It seems like a bug to me. Not much you can do, really. Edit: fixed formatting (hopefully).

    
class MyClass {
private:
MyClass() {}

// undefined

MyClass(const MyClass &);
MyClass &operator = (MyClass);

#ifdef _MSC_VER

public:
#endif
~MyClass() {}

public:
static MyClass &Get_Instance() {
static MyClass instance;
return instance;
}
};




[edited by - Null and Void on July 25, 2002 8:11:33 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Same thing happens to me with VC++. Let me guess: GetInstance cannot access the destructor is your problem.

Just make the destructor public. It works fine otherwise.

-James

"The best thing about C++ is that only your friend s can play with your private parts."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I tried using my Singleton as protected:

--------------------Configuration: KeyMapGenerator - Win32 Debug--------------------
Compiling...
KeyBoardManager.cpp
Generating Code...
Compiling...
SystemData.cpp
C:\Source Control\Xanatos Source Control\KeyMapGenerator\SystemData.cpp(36) : error C2248: ''CSystemData::~CSystemData'' : cannot access protected member declared in class ''CSystemData''
c:\source control\xanatos source control\keymapgenerator\systemdata.h(18) : see declaration of ''CSystemData::~CSystemData''
Generating Code...
Skipping... (no relevant changes detected)
KeyMapGeneratorDlg.cpp
SystemFiles.cpp
Error executing cl.exe.

KeyMapGenerator.exe - 1 error(s), 0 warning(s)

The error was at the end of my GetInstance()

-James

Share this post


Link to post
Share on other sites
quote:
Original post by level10boy
I tried using protected and it still wouldn''t have it. I''m gonna see if it''s on Microsofts bug list.

It should be Microsoft''s deficiency list.

Share this post


Link to post
Share on other sites
Yeah, it was me. MSVC 6.0 can''t handle the destructor being anything but private, even though the singleton accessor function is a member. This is fixed in .NET.

Share this post


Link to post
Share on other sites