如何为代理类实现相等运算符函数
我定义了一个日期类来模拟日期,它有日、月和年作为数据成员。现在比较我创建的相等运算符的日期
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,只需使用运算符即可:
注意
operator==
如何获取引用。这意味着,如果您有一个指针(或一个类似于指针的对象,例如scoped_ptr
或shared_ptr
),那么您必须首先取消引用它:顺便说一下,您应阅读以下内容:运算符重载。
Well, just use the operator:
Notice how the
operator==
takes a reference. That means, if you have a pointer (or an object acting like a pointer such asscoped_ptr
orshared_ptr
), then you have to dereference it first:By the way, you should read this: Operator overloading.
如果正确实现,它应该像任何其他相等运算符一样工作。
尝试:
It should work like any other equality operator if it is implemented correctly.
Try: