class testClass
{
public:
void set(int monthValue, int dayValue);
int getMonth();
int getDay();
private:
int month;
int day;
};
我有一个简单的类,可以在上面看到。我试图将它的对象传递给一个检查它们是否相等的函数。传递类成员void *
testClass obj[3];
obj[0].set(1,1);
obj[1].set(2,1);
obj[2].set(1,1);
首先,我试着像cout << (obj[0] == obj[1]);
,但也不是没有可能运算符重载,使用模板等,所以,我可以用他们做,但我怎么能传递成员变量的void*
功能?
bool cmp_testClass(void const *e1, void const *e2)
{
testClass* foo = (testClass *) e1;
testClass* foo2 = (testClass *) e2;
return foo - foo2; // if zero return false, otherwise true
}
我以为这样想,但我无法解决问题。我想比较像
obj[0].getDay() == obj[1].getDay();
obj[0].getMonth() == obj[1].getMonth();
通过。
究竟是什么问题?我不明白你的问题。 –
这里绝对不需要'void *'。你的'compare_class'可以使用'const DayOfYear&',但是你可能需要使用public getters或者使这个函数成为'friend'。 – crashmstr
如果您不想通过'foo-foo2'进行比较,那么请执行其他操作。我非常怀疑,无论你想要做什么都应该以需要无效指针的方式完成。 – Hurkyl