Jump to content
  • Advertisement
Sign in to follow this  
Tera_Dragon

Singlton class error

This topic is 4843 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

I've got a singlton class that I have been using with no trouble with MSVC++6, but I am getting an error with it when using MSVS2005 beta2. Here is the class file:
#pragma once

#ifndef _SINGLETON_H
#define _SINGLETON_H

template<typename T>
class Singleton{
	static T* singleton;
	public:
		Singleton()
		{
			//use a cunning trick to get the singleton pointing to the start of the whole, rather than
			//the start of the Singleton part of the object
			int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
			singleton = (T*)((int)this + offset);
		}
		~Singleton()
		{
			if(Singleton)
				singleton = NULL;
		}

		static inline T& GetSingleton()
		{
			return *singleton;
		}

		static inline T* GetSingletonPtr()
		{
			return singleton;
		}
};

template <typename T> T* Singleton <T>::singleton = 0;

#endif
And the warnings/errors:
singleton.h(14) : warning C4311: 'type cast' : pointer truncation from 'ModelManager *' to 'int'
singleton.h(14) : warning C4311: 'type cast' : pointer truncation from 'Singleton<T> *' to 'int'

singleton.h(15) : warning C4311: 'type cast' : pointer truncation from 'Singleton<T> *const ' to 'int'
singleton.h(15) : warning C4312: 'type cast' : conversion from 'int' to 'ModelManager *' of greater size

singleton.h(19) : error C2059: syntax error : ')'
What's wrong and how can I fix it?

Share this post


Link to post
Share on other sites
Advertisement
Those are 64bit portability warnings. If you never care that your application will be compiled for a 64bit platform then you can ignore them or disable the /Wp64 flag. Basically the problem is that on 64 bit architectures MSVC uses a 32 bit int, but a 64 bit pointer, so casting a pointer to an int will lose pointer information. To fix this you need to use a type that is 32 bits on x86-32 and 64 bits on x86-64.

Share this post


Link to post
Share on other sites
I think size_t(from cstddef) should be the native pointer size on whatever platform you use.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
I think size_t(from cstddef) should be the native pointer size on whatever platform you use.


Thanks, that's got rid of the warnings.

Share this post


Link to post
Share on other sites
The error is caused by the destructor - try this one:


~Singleton()
{
if(singleton) // it's singleton - not Singleton
singleton = NULL;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Singleton()
{
//use a non-cunning trick to get the singleton pointing to the start of the whole, rather than
//the start of the Singleton part of the object
singleton = static_cast(this);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
static_cast < T* > (this); even

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!