Sign in to follow this  
necreia

C++, class value casting (operator overloading)

Recommended Posts

Hello, hopefully this question will not be too complex. We are using a class (will call it CVariable) which is made up as follows (including the type enumeration):
enum Type {TYPE_INT = 1, TYPE_FLOAT, TYPE_STRING, TYPE_MATRIX33,
TYPE_MATRIX44, TYPE_HANDLE, TYPE_VECTOR4, TYPE_VECTOR3,	TYPE_VECTOR2 };

class CVariable
{
public:
	CVariable();
	~CVariable();

	void SetName(sfString Name);
	void *mValue;
	String mName;
	int mType;
};
The idea is, that we want to be able to use this in a variable manager, so we want it to manage it's own values. We tried using operator overloading to assign and retrieve the values, but we cannot retrieve them. Here is an example:
inline int CVariable::operator = (const int Int)
{
	// free the previous memory
	if (mValue)
		free(mValue);

	// allocate the memory and copy
	mValue = malloc(sizeof(int));
	CopyMemory(mValue, &Int, sizeof(int));

	// return this int
	return (int)((INT_PTR)mValue);
}

And here is where we try to use it:
sfVariable TestVariable;
TestVariable = 44;

int TestInt2 = TestVariable; // error, cannot convert

Obviously there is an error with this, is there any way to create an overload to handle that type of call? We can use member functions such as "void* GetValue()" and cast on the fly, or use "int GetInt(), float GetFloat()" etc, but we'd like to use a method more generic/seamless if possible.

Share this post


Link to post
Share on other sites
You can use conversion operators. ex:

class CVariable
{
public:
// blah blah blah

operator int() const {
return (int)((INT_PTR)mValue);
}
};


edit: keep in mind like most automatic conversions, this kind of thing can lead to strange and subtle errors.

Share this post


Link to post
Share on other sites
This is what I use in my code base...


class AbstractProperty
{
public:
virtual ~AbstractProperty() {}
};



template <class T> class Property : public AbstractProperty
{
public:
/// Construct a property with an initial value
Property(const T& value) : m_value(value) {}
/// Destroy property
~Property() {}

/// Set property value
void SetValue(const T& value) { m_value = value; }
/// Get property value
const T& GetValue() const { return m_value; }

private:

/// Property value
T m_value;
};



Then I have a Registry class that stores keyed pairs of Property objects using a Hash of their name...

The registry class is basically a wrapper for this data structure...

typedef std::map<HashedKey, AbstractProperty*> PropertyMap;

.. and the public methods are...


/// Set a registry value
template <class T> void SetValue(const char* key, const T& value);
/// Get a registry value
template <class T> const T* GetValue(const char* key);



SetValue, looks up the key in the map, and changes the value in the Property if found, or creates a new Property and adds it to the map if not.

GetValue, returns 0 if the key is not found, otherwise a pointer to the value.

Obviously there are many possible variants on the above, but I would definetly recommend using templates over having Enums for all the types.




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this