如何在cpp中比较带有void *的两个类。
我比较日期和月份。
我可以在compare_dayOfYear函数中做什么?

这是我的课:

class DayOfYear
{

public:
    DayOfYear(int newMonth,int newDay);
    //Initializes the month and day to arguments.

    DayOfYear(int newMonth);
    //Initializes the date to the first of the given month.

    DayOfYear();
    //Initializes the date to January 1.

    // Accessors
    int getMonthNumber( );
    //Returns 1 for January, 2 for February, etc.
    int getDay( );

    // Mutators
    int setMonthNumber(int newMonth);
    //Set 1 for January, 2 for February, etc.
    int setDay(int newDay);

private:
    int month;
    int day;
};


bool compare_dayOfYear(const void * firstValue, const void * secondValue)
{
    //I compare day and month
    //What can I do here
}


如何在cpp中比较带有void *的两个类。
我比较日期和月份。
我可以在compare_dayOfYear函数中做什么?

最佳答案

如果确定已经传递了DayOfYear对象,则可以只转换void指针。

bool compare_dayOfYear(const void * firstValue, const void * secondValue)
{
  DayOfYear* firstDayOfYear = static_cast<DayOfYear*>(firstValue);
  DayOfYear* secondDayOfYear = static_cast<DayOfYear*>(secondValue);
  //compare..
}

09-04 17:41