Skip to main content
gamedev.net
🔒 Locked

C++ overloading default type operators ?

Started by DMINATOR Oct 25, 2005 at 6:21 AM 5 replies 450+ views
Original Post
DMINATOR
DMINATOR
I am just curious. Can I somehow overload default type operators. For example I want to have a class class foo { int a; }; And once the variable is Going to be changed I want to call my function and only then decide what other params I need to change. This is something similar with C# Get() Set(). I have no idea how can I overload it :) Any help appreciated.
stylin
stylin
You most certainly can overload operators. You also can write get()/set() member functions. What exactly do you want to do? Do you want to have a be a certain type depending on parameters you give foo? Do you just want to gain control over the access of your data members (reading/writing)?
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
ZQJ
ZQJ
As far as I'm aware you can't. However you can do this:

class foo{  struct a_struct  {    int value;    operator const int& () const {return value;}    int operator = (int value) {/* Your code here */}  } a;};


You'll have to define a number of operators to make this work like an int: =, *=, +=, -=, /=, ++, -- should hopefully do. I'm not sure how the compiler would handle automatic type conversion for something like this:

foo x, y;
int c = x.a * y.a;

Which could really complicate your life. On the other hand it shouldn't be too hard to write a templated class to handle most of that stuff for multiple types. This looks like you'll probably need to keep a copy of the this pointer in a_struct which is bad but I'm sure there's a way around that.
Kalasjniekof
Kalasjniekof
The only way to do that would be:

class foo{public:    int GetA( )     {         return m_a;     }    void SetA( int a_int )    {        if( Valid( a_int ) )        {            m_a = a_int;        }    }private:     int m_a;};


Where "bool Valid()" is any condition you impose on a_int.
Remember, you can only overload operators on classes (I read somewhere that you can overload the global new and delete operators though).
DMINATOR
DMINATOR
Thanks for the comments everyone.

2 stylin and Kalasjniekof

What I won't to do, is to use the most simple way for working with data, like
foo.a = 10, not just foo.SetA(10).

2 ZQJ

Yes, this is it, but you are right the structures are going to add some nice complexity to the code.
Maybe anyone knows some other way ?
Arek the Absolute
Arek the Absolute
If the data member is an object rather than a primitive like an integer, it's possible to do what you want through operator overloading. If you really do just want an integer you could still wrap it in a class for this purpose. For instance, say you have these classes:

class something {public:    something &operator = (int n){         data = n;         // whatever other code you want also    }private:    int data;};class foo {public:    something a;};


Then it would be possible to make the assignment like this:
foo foobar;
foobar.a = 10;

And foobar.a = 10 will run the overloaded operator specified, which could be much more code than the simple assignment.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.