我是C ++的新手,但是已经使用python了一段时间。.我无法弄清楚...
我尝试使用“ this”指针,但出现错误,因此我想知道是否有人可以将我指向正确的方向。
谢谢
这是我想要实现的目标:
我有一个约会和一个人班,并且我必须添加一个班级方法来检查该人是成人还是儿童
到目前为止,这是我的方法:
bool isAdult()
{
util::Date today = util::Date();
int age = today.getYear()- this.getYear();
if (today.getMonth() < this.getMonth() || (today.getMonth() == this.getMonth() && today.getDay() < this.getDay()))
{
age = age -1;
}
if (age >= 18)
{return true;}
else{return false;}
}
我怎么能做这样的事情:
ostringstream os;
os << getDay() << " \n";
os << getMonth() << " \n";
os << getYear();
但是我不能这样做:
long year = getYear();
最佳答案
在C ++中,this
是指针类型,因此您可能需要取消引用它才能调用成员。
有两种取消引用成员调用指针的方法:
// Using the * operator to get the actual instance
auto result = (*this).do_something();
// Using the -> operator to dereference and get/invoke member simultaneously
auto result = this->do_something();
关于c++ - C++等同于self.object(python),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21833933/