本文介绍了为什么静态成员函数会那样工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,最近我遇到一个问题:
hello,erveryone,recently I meeted a question:
#include <iostream.h>
#include <string.h>
class MyTime
{
public:
char m_MyName[100];
static int m_count
MyTime(char *p1,char *p2,int i,int j,int k);
~MyTime();
void ShowTime();
static void GetCount();
static void GetTime(MyTime);
protected:
char m_MyGender[5];
private
int m_Hour;
int m_Minute;
int m_Second;
};
int MyTime::m_count=0;
MyTime::MyTime(char *p1,char *p2,int i,int j,int k)
{
strcpy(m_MyName,p1);
strcpy(m_MyGender,p2);
m_Hour=i;
m_Minute=j;
m_Second=k;
}
void MyTime::ShowTime()
{
cout<<m_MyName<<" tell:it is:"<<m_Hour<<":"<<m_Minute<<":"<<m_Second<<endl;
}
MyTime::~MyTime()
{
m_Hour=m_Hour+1;
m_Minute=m_Minute+1;
m_Second=m_Second+1;
ShowTime();
}
void MyTime::GetCount()
{
cout<<"m_count="<<m_count<<endl;
}
void MyTime::GetTime(MyTime t)
{
cout<<"time: "<<t.m_Hour<<":"<<t.m_Minute<<":"<<t.m_Second<<endl;
}
void main()
{
char *p1="sunshine";
char *p2="man";
MyTime mytime(p1,p2,10,10,10);
MyTime::m_count++;
mytime.GetCount();
MyTime::GetTime(mytime);
}
The result is:
m_count=1
time: 10:10:10
sunshine tell:it is:11:11:11
sunshine tell:it is:11:11:11
Press any key to continue</string.h></iostream.h>
刚才我很困惑为什么"sunshine tell:it is:11:11:11"出现两次.检查代码后,我发现这是〜MyTime()起作用两次的原因.当我删除该代码时"MyTime :: GetTime(mytime)",它正常工作.
现在我不知道为什么它会那样工作,你呢?我期待着您的答复! ^ _ ^^ _ ^^ _ ^
Just now,I was confused that why "sunshine tell:it is:11:11:11" appeared twice.After checking the code,I found it was the reason of ~MyTime(),which worked twice.And when I deleted the "MyTime::GetTime(mytime)",it worked correctly.
Now I don''t know why it works like that,And you? I''m looking forward to your answer! ^_^^_^^_^
推荐答案
static void GetTime(const MyTime & t);
(实现)
(implementation)
void MyTime::GetTime(const MyTime & t)
{
//...
}
如果您不喜欢这样的行为.
if you don''t like such a behaviour.
这篇关于为什么静态成员函数会那样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!