Why no default == operator?

It’s understandable that C++ didn’t provide default operator == (does provide =) because C did the same thing; C++ has to be compatible with C. But if we go one step further, why does C provide only default = and no ==?
C wanted to keep it simple:
C implemented = by memcpy; however, == cannot be implemented by memcmp.
See this example:

struct CMyClass
	int a;
	char b;
int main()
	struct CMyClass My1, My2;
	My1.a = 2; My1.b = 2;
	My2.a = 2; My2.b = 2;
	int iSize = sizeof(struct CMyClass);//With default #pragma pack(8)
	printf("sizeof(CMyClass)%d\n", iSize); //prints 8
	int iResult = memcmp(&My1, &My2, sizeof(struct CMyClass));
		printf("My1 != My2");//this gets printed even though both are initialized to same value

This snippet can be run from here: codepad.org/aTQKorKc
“My1 != My2” because of the paddings.
If we do a “My1 = My2;”, the result would be “My1 == My2”, and this proves memcpy is used to implement = operator.
This explains why C didn’t implement operator ==. Your correction is appreciated if I’m wrong.

Comments are closed.
%d bloggers like this: