M
Mark A. Gibbs
Given this situation:
class Base
{
public:
virtual ~Base();
virtual bool equals(Base const&) const = 0;
};
Base::~Base() {}
inline bool operator==(const Base& a, const Base& b)
{
return a.equals(b);
}
class Derived : public Base
{
public:
bool equals(const Base& b) const
{
if (typeid(*this) == typeid(b))
{
return data_ == b.data_;
}
return false;
}
private:
int data_;
};
// in use
Base& a(getA());
Base& b(getB());
return a == b;
Is there any way I can do this without RTTI?
I was thinking of something like:
class Base
{
public:
virtual ~Base();
virtual bool equals(Base const&) const = 0;
protected:
virtual void* type_() const = 0;
};
inline bool operator==(const Base& a, const Base& b)
{
return a.equals(b);
}
class Derived : public Base
{
public:
bool equals(Base const&) const;
protected:
void* type_() const { return &i; }
private:
static int const i;
};
int const Derived::i = 0;
But are there any other ways to do this?
indi
class Base
{
public:
virtual ~Base();
virtual bool equals(Base const&) const = 0;
};
Base::~Base() {}
inline bool operator==(const Base& a, const Base& b)
{
return a.equals(b);
}
class Derived : public Base
{
public:
bool equals(const Base& b) const
{
if (typeid(*this) == typeid(b))
{
return data_ == b.data_;
}
return false;
}
private:
int data_;
};
// in use
Base& a(getA());
Base& b(getB());
return a == b;
Is there any way I can do this without RTTI?
I was thinking of something like:
class Base
{
public:
virtual ~Base();
virtual bool equals(Base const&) const = 0;
protected:
virtual void* type_() const = 0;
};
inline bool operator==(const Base& a, const Base& b)
{
return a.equals(b);
}
class Derived : public Base
{
public:
bool equals(Base const&) const;
protected:
void* type_() const { return &i; }
private:
static int const i;
};
int const Derived::i = 0;
But are there any other ways to do this?
indi