A rather long while ago, I published a little teaser on std::set and people seemed to like it quite a bit. So here goes another one based on a problem a friend has found at work today. I hope to reproduce the main idea behind the problem correctly, but my memory is a bit fuzzy.

Can you guess why the following program fails to compile due to an error in the call to equals from within main? Bonus points if you don't build it.
struct data {
int field;
};

template< typename Data >
class base {
public:
virtual ~base(void)
{
}

virtual bool equals(const Data& a,
const Data& b) const
{
return a == b;
}
};

class child : public base< data > {
public:
bool equals(const data& a,
const data& b) const
{
return a.field == b.field;
}
};

int
main(void)
{
data d1, d2;
base< data >* c = new child();
(void)c->equals(d1, d2);
delete c;

return 0;
}
Tip: If you make base::equals a pure abstract method, the code builds fine.

Go to posts index

Comments from the original Blogger-hosted post: