I am wondering if it is safe to case one class to binary-equal class that is nevertheless not the same class.
Background: I want to do some operator-magic without "polluting" the original class (here A).
class A
{
public:
int x;
virtual ~A() = default;
};
class B : public A
{
public:
B(int x) { this->x = x; }
bool operator==(const B& b)
{
return x == b.x;
}
};
A a;
a.x = 1;
B* b = (B*)&a;
*b == B(1);
This works on all my systems, but is it safe/portable to do?