36 lines
1.1 KiB
C++
36 lines
1.1 KiB
C++
#ifndef CC_OBJECT_H
|
|
#define CC_OBJECT_H
|
|
#pragma once
|
|
|
|
#include "CC.h"
|
|
#include <functional> // 用于 std::hash
|
|
#include <string> // 用于 std::string
|
|
#include <memory> // 用于 std::addressof
|
|
|
|
namespace CTL {
|
|
class Object {
|
|
public:
|
|
virtual ~Object() = default; // 虚析构函数确保正确析构
|
|
// 比较两个对象是否逻辑相等(默认实现为地址比较)
|
|
virtual bool equals(const Object* other) const {
|
|
return this == other; // 默认比较内存地址
|
|
}
|
|
// 生成基于对象地址的哈希值
|
|
[[nodiscard]] virtual size_t hashCode() const noexcept {
|
|
return std::hash<const Object*>{}(this);
|
|
}
|
|
// 返回对象的字符串表示(默认格式:类名@地址)
|
|
[[nodiscard]] virtual std::string toString() const {
|
|
char buffer[64];
|
|
snprintf(buffer, sizeof(buffer), "Object@%p", static_cast<const void*>(this));
|
|
return buffer;
|
|
}
|
|
friend std::ostream& operator<<(std::ostream& os, const Object& map) {
|
|
os << map.toString();
|
|
return os;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif
|