Define generic comparison operator

前端 未结 4 1990
[愿得一人]
[愿得一人] 2021-01-07 19:22

I came up with the idea to define a generic comparison operator which would work with any type, for the fun of it.

#include 
#include 

        
4条回答
  •  悲&欢浪女
    2021-01-07 20:10

    Let's take a perfectly normal class, say String. It's implemented as you'd think, with a char* that points to a new[]'ed buffer.

    Now compare two of them. Obviously, String("abc")==String("abc"). Yet your implementation fails this test, as the two pointers differ.

    Equality is defined by the class semantics, not by the bits directly inside the object.

提交回复
热议问题