C++getter函数返回0

C++ - getter function returns 0?

本文关键字:返回 函数 C++getter      更新时间:2023-10-16

类:

class Process
{
public:
   void setTime(int temp);
   int getTime();
private:
   int myTime;    
}

的功能

Process::Process()
{
   myTime = 0;
}
void Process::setTime(int temp)
{
   myTime = temp;
}
int Process::getTime()
{
   return myTime;
}

Main只有Process对象,并设置一个数字和get。我和几个朋友一直在试图弄清楚为什么get函数返回零。

实际的代码在我的另一台电脑上,我现在无法访问它。

如果有帮助的话,我会在一段时间内做一些事情。有什么想法吗?

我认为您需要在类本身内部定义构造函数,因为变量是私有的。。或者尝试使用this.myTime..,或者可能是因为您在set之前调用了get方法。。。