Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>

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! ^_^^_^^_^
Posted

This is because the object is destroyed. Check out what happens in the destructor ~MyTime() ;-) It looks like the destructor is already called and destruction and copying gets somewhat tangled up. You could output some extra information to the screen (like from the destructor) to see what is happening when.

Good luck!
 
Share this answer
 
v2
Comments
Rslboy 2-Aug-11 9:00am    
Thanks for your answer!it is the reason of the way the static member function(GetTime)is defined.As Cpallini said,I got the resolution!:-):-)
It happens because GetTime method gets its parameter by value, hence a temporary object is created (and then destroyed when such method terminates).
Change GetTime signature (updated as per Philippe Mori suggestion)

(prototype in class)
C++
static void GetTime(const MyTime & t);


(implementation)
C++
void MyTime::GetTime(const MyTime & t)
{
  //...
}


if you don't like such a behaviour.
 
Share this answer
 
v8
Comments
Rslboy 2-Aug-11 8:56am    
Thanks very much!the resolution you said is right for that,谢谢!:-):-)
CPallini 2-Aug-11 9:00am    
So you gave me 1?
:-)
_Zorro_ 2-Aug-11 9:04am    
+5, there you go :)
CPallini 2-Aug-11 9:05am    
Thank you, masked hero!
Philippe Mori 2-Aug-11 22:12pm    
Should be static void GetTime(const MyTime & t) since you don't want to modify the time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900