头文件(.h):
bool canTravelWith(const Passenger&) const;
实现文件(.cpp):
bool Passenger::canTravelWith(const Passenger& x) const
{
bool canTravel = false;
//if both passengers have the same destination on the same date...
if (strcmp(x.m_destination,this->m_destination) == 0 && x.m_year == this->m_year && x.m_month == this->m_month && x.m_day == this->m_day)
{
canTravel = true;
}
return canTravel;
}
大家好,
上面的代码可以工作,但我想知道的是参数的对象的成员是否是私有(private)访问的;我如何能够在 canTravelWith() 中调用该对象的成员?
任何其他情况;我无法调用对象的私有(private)成员。
我想了解这是为什么。
谢谢。 (:
最佳答案
private
和 protected
的定义不会限制您从同一类访问这些类型的属性或函数。 p>
这是一个 Passenger
函数,因此它可以完全访问所有内容。游戏中有两个对象实例并不限制您。 private
并不意味着其他实例不能触及它,只能是其他类不能触及它。
关于c++ - 实例对象如何查看函数的对象参数的私有(private)成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611170/