gpt4 book ai didi

c++ - 如何在 cpp 中将两个类与 void* 进行比较

转载 作者:行者123 更新时间:2023-11-28 06:00:45 25 4
gpt4 key购买 nike

我如何在 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..
}

关于c++ - 如何在 cpp 中将两个类与 void* 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33329439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com