   c++ - operator overload
  Hello.I'm writing a class which basically needs to deal with a number with private scope.The problem is, how can I assign to an object of that class a sort of pre-defined value? Consider the following code:void somefunction() {    MyClass c;    int n = 200;    c = 100; //Ok, it works, as I overloaded the assignment operator    c = n; //Still working    n = c; // ouch !}I think the problem is fairly clear here. Moreover, the class I'm writing is a template, thus n can really be any type, and not only an int. Well, is there a way to make the statement <typename> = <object> valid?   
  Presumably, there is some variable in your class which is getting assigned to n in the firstplace when you call c = n.In that case, if the variable is public, just use:n = c.Variable;Or, if its private, write a getter:n = c.getVariable();   ;  You can also write an int operator:class Test {public:    operator int() { return 10; }};int main() {    Test t;    int a=t;    return 0;};  Ok, for the sake of simplicity, forget the template thing, and let's assume it's a normal class. See it as:class MyClass {public:    MyClass& operator= ( int n ) { m_nVal = n; };private:    int m_nVal;};I *could* use a GetValue() function, but since I've been having this doubt forever, I'd like to find out if there's a better workaround. Basically, what I want to achieve is the same as setting a default property in Visual Basic, where for example you can assign the .Text property of a TextBox to a string without actually specifying the .Text property.The problem, in this case, gets bigger because I only want to overload operators once:class MyClass {public:    MyClass& operator= ( int n ) { m_nVal = n; };    MyClass& operator+= ( int n );private:    int m_nVal;};Obviously, this makes the use of += operator invalid between two objects of the same type, unless I write another operator+= function getting a MyClass& argument.   ;  Wow, what a quick reply :D I'm trying that at once. Thanks!   ;Quote:Original post by Anonymous PosterYou can also write an int operator:class Test {public:    operator int() { return 10; }};int main() {    Test t;    int a=t;    return 0;}edit: wrote this before the OP said to forget about templatizing, but its still something to keep in mind if templatizing the class may happen.The OP said that his class is templatized, so n is not always an int. Casting operators can't be templatized (ie, you can't just operator T(), I'm pretty sure), so you'd have to write one for each data type, which wouldn't work if using user defined data types, though it might be acceptable if only dealing with floats/ints/doubles.   ;  I just tried to compile, and although there's a lineinline operator T ( void );being T the template typename, it works fine. Normally, this class should be used with integer types, so I think I'll be safe for now, but I'll keep in mind to be careful.In case you want to know, the compiler I'm using is ProDG.Thanks for helping! :)   ;Quote:Original post by Anonymous PosterYou can also write an int operator:class Test {public:    operator int() { return 10; }};int main() {    Test t;    int a=t;    return 0;}Automatic casts to standard types should be avoided. In fact, automatic casts should be avoided when possible (whatever the destination type is). They can lead to weird problems and are not very easy to read. 1) if your destination type is a standard type (int, bool, float, char, double, ...) then you can easily provide a function to do the job (to_int, to_bool, and so on). This will ensure that your code will be explicit (remember that while writing code is not easy, reading code is even harder - the more you simplify code reading the better it is).2) if your destination type is a class or a struct, it can provide a constructor which take an parameter of the source type.In my whole C++ experience, I never saw a case where automatic cast was really needed. Regards,   ;  You can define custom assignment operators that are non-member functions:int& operator=(int& x, const test& y){    x = y.a_member_func();}Quote:In my whole C++ experience, I never saw a case where automatic cast was really needed.Generally, they're useful in situations where a conversion is 100% safe:Some examples:Conversion from an iterator type to a const_iterator typeConverion from smart_pointer<derived_class> to smart_pointer<base_class>   ;Quote:Original post by NitageYou can define custom assignment operators that are non-member functions:*** Source Snippet Removed ***Quote:In my whole C++ experience, I never saw a case where automatic cast was really needed.Generally, they're useful in situations where a conversion is 100% safe:Some examples:Conversion from an iterator type to a const_iterator typeConverion from smart_pointer<derived_class> to smart_pointer<base_class>Not with my compiler:Quote:Source: Gcc170 C:\Programming\c++\tests\main.cpp `int& operator=(int&, const test&)' must be a nonstatic member function class test{public:    int a_member_func() const{ return 10;}};int& operator=(int& x, const test& y){    x = y.a_member_func();}