如何为代理类实现相等运算符函数

发布于 2024-12-15 05:16:10 字数 1771 浏览 2 评论 0原文

我定义了一个日期类来模拟日期,它有日、月和年作为数据成员。现在比较我创建的相等运算符的日期

bool Date::operator==(const Date&rhs)
{
    return (rhs.day()==_day && _month==rhs.month() &&_year==rhs.year());
}

现在如何从 Proxy 类调用 Date 类相等运算符...?


这是问题的编辑部分

这是日期类

//Main Date Class
enum Month 
{
    January = 1, 
    February,
    March, 
    April,
    May,
    June,
    July, 
    August, 
    September,
    October,
    November, 
    December
};
class Date
{
public: 

    //Default Constructor
    Date();
    // return the day of the month
    int day() const
    {return _day;}
    // return the month of the year
    Month month() const
    {return static_cast<Month>(_month);}
    // return the year
    int year() const
    {return _year;}

    bool Date::operator==(const Date&rhs)
    {
    return (rhs.day()==_day && _month==rhs.month() &&_year==rhs.year());
    }

    ~Date();

private:
    int _day;
    int _month;
    int _year;

}

//--END OF DATE main class

这是我将替换日期类的代理类

//--Proxy Class for Date Class
class DateProxy
{
public: 

    //Default Constructor
    DateProxy():_datePtr(new Date)
    {}
    // return the day of the month
    int day() const
    {return _datePtr->day();}
    // return the month of the year
    Month month() const
    {return static_cast<Month>(_datePtr->month());}
    // return the year
    int year() const
    {return _datePtr->year();}

    bool DateProxy::operator==(DateProxy&rhs)
    {
        //what do I return here??
    }

    ~DateProxy();

private:
    scoped_ptr<Date> _datePtr;

}

//--End of Proxy Class(for Date Class)

现在我遇到的问题是在代理类中实现相等运算符函数,我希望这能澄清问题。

I have a class Date that I defined that models a date, it has a day,month and a year as data members. Now to compare the dates I created the equality operator

bool Date::operator==(const Date&rhs)
{
    return (rhs.day()==_day && _month==rhs.month() &&_year==rhs.year());
}

Now how do I call the Date class equality operator from the Proxy class...??


This is the edited part of the question

This is the Date Class

//Main Date Class
enum Month 
{
    January = 1, 
    February,
    March, 
    April,
    May,
    June,
    July, 
    August, 
    September,
    October,
    November, 
    December
};
class Date
{
public: 

    //Default Constructor
    Date();
    // return the day of the month
    int day() const
    {return _day;}
    // return the month of the year
    Month month() const
    {return static_cast<Month>(_month);}
    // return the year
    int year() const
    {return _year;}

    bool Date::operator==(const Date&rhs)
    {
    return (rhs.day()==_day && _month==rhs.month() &&_year==rhs.year());
    }

    ~Date();

private:
    int _day;
    int _month;
    int _year;

}

//--END OF DATE main class

This is the proxy class that I will substitute for the Date class

//--Proxy Class for Date Class
class DateProxy
{
public: 

    //Default Constructor
    DateProxy():_datePtr(new Date)
    {}
    // return the day of the month
    int day() const
    {return _datePtr->day();}
    // return the month of the year
    Month month() const
    {return static_cast<Month>(_datePtr->month());}
    // return the year
    int year() const
    {return _datePtr->year();}

    bool DateProxy::operator==(DateProxy&rhs)
    {
        //what do I return here??
    }

    ~DateProxy();

private:
    scoped_ptr<Date> _datePtr;

}

//--End of Proxy Class(for Date Class)

Now the problem that I am having is implementing the equality operator function in the proxy class, I hope this clarifies the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒几许 2024-12-22 05:16:10

好吧,只需使用运算符即可:

Date d1, d2;
if(d1 == d2)
    // ...

注意 operator== 如何获取引用。这意味着,如果您有一个指针(或一个类似于指针的对象,例如 scoped_ptrshared_ptr),那么您必须首先取消引用它:

*_datePtr == *rhs._datePtr;

顺便说一下,您应阅读以下内容:运算符重载

Well, just use the operator:

Date d1, d2;
if(d1 == d2)
    // ...

Notice how the operator== takes a reference. That means, if you have a pointer (or an object acting like a pointer such as scoped_ptr or shared_ptr), then you have to dereference it first:

*_datePtr == *rhs._datePtr;

By the way, you should read this: Operator overloading.

手心的海 2024-12-22 05:16:10
return *_datePtr == *_rhs.datePtr;
return *_datePtr == *_rhs.datePtr;
蘸点软妹酱 2024-12-22 05:16:10

如果正确实现,它应该像任何其他相等运算符一样工作。

尝试:

Date a = //date construction;
Data b = //date construction;

if(a == b)
    printf("a and b are equivalent");
else
    printf("a and b are not equivalent");

It should work like any other equality operator if it is implemented correctly.

Try:

Date a = //date construction;
Data b = //date construction;

if(a == b)
    printf("a and b are equivalent");
else
    printf("a and b are not equivalent");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文