# When does the compiler generate operators automaticly?

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

## Recommended Posts

Hello, i have question about operators. I dont like to define operators myself since its easy to make bugs if you add new members to the class and forget to update the operators. So im very happy when the compiler automaticly generates operators like = == and !=. It seems to work my the simple rule of just performing the same operator on all the class's member variables. Which is great, since that is all you would probebly ever do by implementing it yourself anyway. But sometimes.. It dosnt generate operators? You get a compiler(MSVC.NET) error. For instance my very simple box class which only contains two members of the type vertex3 generates this error. What are the rules when operators gets generated and when they dont? My box class would work perfectly if the == operator would just preforming the == operator on all its members aswell.. Can you force this with some compiler flag or something?
struct box_t : public intersection_volume_t // intersection volume is just a pure virtual function get_volume_type();
{
volume_type_t get_volume_type() const;
// data
vector3_t	m_min;
vector3_t	m_max;
};

const box_t* a; const box_t* b;
if (*a == *b)

//The error:
//"intersection_tests.cpp(144): error C2676: binary '==' : 'const box_t' does not define this operator or a conversion to a type acceptable to the predefined operator"



##### Share on other sites
The operators it can generate generally depend on the class members; for instance, a class with reference members won't have an autogenerated assignment operator. Post your class definition.

##### Share on other sites
1) Operators can only be generated for primitive data types.
That means if you have a struct or a class, the compiler cannot generate a comparison, addition or multiplication operator.
2) The assignment operator can always be generated but since 1) still applies, complex types are copied member-wise, which usually results in unexpected behaviour if you have arrays or pointers inside your struct/class. (e.g. it doesn't work with dynamic arrays the way you would expect it - it would just copy over the pointer).

OK - Sneftel is right - references don't work with the assignment operator [smile]
[/edit]

##### Share on other sites
You have to provide a custom == operator.
struct box_t : public intersection_volume_t // intersection volume is just a pure virtual function get_volume_type();	{volume_type_t get_volume_type() const;		// data		vector3_t	m_min;		vector3_t	m_max;	};inline bool operator == (const box_t& a, const box_t& b) {   return a.m_min == b.m_min && a.m_max == b.m_max;}inline bool operator != (const box_t& a, const box_t& b) {   return !(a == b);}

This will only work if vector3_t have an equal operator as well.

##### Share on other sites
The box_t was a bad example since it depends on alot of classes.

lets take the vector3_t class instead:
struct vector3_t	{		vector3_t();		vector3_t(const vector3_t& vec);		vector3_t(float x, float y, float z);		virtual ~vector3_t();		// equality		bool operator==(const vector3_t& src) const;		// inequality		bool operator!=(const vector3_t& src) const;		// data		float x, y, z;			};

if i comment out the == and != operators, those operators wont work for the vector3_t class anymore. In this case when its a this simple class, with only basic types as members, why cant the compiler generate those operators?

I just think operators are so confusing.. i mean i have two times made really hard to track bugs that took hours, because the class was modified but not the operators. Its like adding this extra layer of dependance, if you change something, you have to change it in several places. It would be so much easier if expected behaviur could just be generated by the compiler. :-)

Not to mention that there seems to be lika a million variations of each operator. I often see several lines of declarations for the same == or whatever operator with small diffrances, a const here, a reference there.. etc.. There are so many there is no point trying to be good and define them all for all your classes.

It just seems like hell! :-)

##### Share on other sites
Trying to compare floating point numbers for equality is a pain regardless.

• 32
• 15
• 23
• 10
• 19