对象获取与第一个 C++ 相同的数据

Object gets the same data as the first one C++

我对此有疑问class。当我只创建这个 class 的一个对象时,一切正常。它向我显示了它应该显示的日期。但是每当我尝试创建第二个对象并打印出日期时,它只会向我显示第二个对象的数据。我不知道我做错了什么。

 #include <iostream>
    #include <string>
    #include <time.h>
    #include <conio.h>

using namespace std;


class convData {


public:

//constructori

    convData(long dataUnix){

        this->mytime = dataUnix;
        this->mytm = NULL;
        this->mytm = gmtime(&this->mytime);
        this->wDay = NULL;
        cout <<endl<<"Address:"<< &this->mytime;//noticed that this address is exactly the same as the one of the object created in main


    }

    ~convData(){
        if (this->wDay != NULL)
            delete[] wDay;
    }


//interfata 

    void setDataUnix(long dataUnix){
        if (dataUnix >= 0)
            this->mytime = dataUnix;
        else
            throw new exception("Data incorecta");

    }
    void setSec(int sec)
    {
        if (sec >= 0 && sec <= 59)
            this->mytm->tm_sec = sec;
        else
            throw new exception("Numarul de secunde este incorect.");

    }
    void setMin(int min){

        if (min >= 0 && min <= 59)
            this->mytm->tm_min = min;
        else
            throw new exception("Numarul de minute este incorect.");

    }
    void setHour(int hour)
    {
        if (hour >= 0 && hour <= 23)
            this->mytm->tm_hour = hour;
        else
            throw new exception("Ora este gresita.");

    }
    void setMday(int mDay)
    {
        if (mDay >= 1 && mDay <= 31)
            this->mytm->tm_mday = mDay;
        else
            throw new exception("Ziua lunii incorecta.");
    }
    void setMonth(int month)
    {
        if (month >= 1 && month <= 11)
            this->mytm->tm_mon = month - 1;
        else
            throw new exception("Luna este gresita");
    }
    void setYear(int year)
    {
        if (year >= 1970 && year <= 9999)
            this->mytm->tm_year = year - 1900;
        else
            throw new exception("Anul este gresit");

    }
    void setWeekDay(string weekDay){

        if (weekDay.compare("Duminica") == 0)
            this->mytm->tm_wday=0;
        else
            if (weekDay.compare("Luni") == 0)
                this->mytm->tm_wday = 1;
        else
        if (weekDay.compare("Marti") == 0)
            this->mytm->tm_wday = 2;
        else
        if (weekDay.compare("Miercuri") == 0)
            this->mytm->tm_wday = 3;
        else 
            if (weekDay.compare("Joi") == 0)
            this->mytm->tm_wday = 4;
        else
        if (weekDay.compare("Vineri") == 0)
            this->mytm->tm_wday = 5;
        else
        if (weekDay.compare("Sambata") == 0)
            this->mytm->tm_wday = 6;
        else
            throw new exception("Ziua saptamanii incorecta.");
    }
    void setYearDay(int yearDay){
        if (yearDay >= 1 && yearDay <= 366)
            this->mytm->tm_yday = yearDay - 1;
        else
            throw new exception("Ziua din an incorecta.");


    }
    void setDST(int DST)
    {
        if (DST == -1)
            this->mytm->tm_isdst = DST;
        else
        if (DST == 0)
            this->mytm->tm_isdst = DST;
        else
        if (DST >= 1)
            this->mytm->tm_isdst = DST;
        else
            throw new exception("DST gresit.");
    }

    int getSec()
    {
        return this->mytm->tm_sec;
    }
    int getMin()
    {
        return this->mytm->tm_min;
    }
    int getHour(){
        return this->mytm->tm_hour;
    }
    int getMday(){
        return this->mytm->tm_mday;
    }
    int getMonth(){
        return this->mytm->tm_mon+1;
    }
    int getYear(){
        return (this->mytm->tm_year + 1900);
    }
    char * getWeekDay(){

        if (this->mytm->tm_wday == 0)
        {
            this->wDay = new char[strlen("Duminica") + 1];
            strcpy(this->wDay, "Duminica");
            return this->wDay;

        }
        else if (this->mytm->tm_wday == 1)
        {

            this->wDay = new char[strlen("Luni") + 1];
            strcpy(this->wDay, "Luni");
            return this->wDay;
        }

        else if (this->mytm->tm_wday == 2)
        {
            this->wDay = new char[strlen("Marti") + 1];
            strcpy(this->wDay, "Marti");
            return this->wDay;

        }
        else if (this->mytm->tm_wday == 3)
        {
            this->wDay = new char[strlen("Miercuri") + 1];
            strcpy(this->wDay, "Miercuri");
            return this->wDay;
        }
        else if (this->mytm->tm_wday == 4)
        {
            this->wDay = new char[strlen("Joi") + 1];
            strcpy(this->wDay,"Joi");
            return this->wDay;
        }
        else if (this->mytm->tm_wday == 5)
        {
            this->wDay = new char[strlen("Vineri") + 1];
            strcpy(this->wDay, "Vineri");
            return this->wDay;
        }
        else if (this->mytm->tm_wday == 6)
        {
            this->wDay = new char[strlen("Sambata") + 1];
            strcpy(this->wDay, "Sambata");
            return this->wDay;
        }
        else
            return "[=10=]";

}
    int getYearDay()
    {
        return this->mytm->tm_yday + 1;
    }
    int getDST(){
        return this->mytm->tm_isdst;
    }
    long getDataUnix(){
        long dataUnix = this->mytime;
        return dataUnix;
    }



    void afisareData(){


                cout << endl << getWeekDay();
                cout << " " << getMday()<<  "-"<< getMonth();
                cout << "-" << getYear() << " " << getHour() << ":";
                if (getMin() < 10)
                    cout << "0" << getMin();
                else
                    cout << getMin();
                if (getSec() < 10)
                    cout << ":0" << getSec();
                else
                    cout<<":" << getSec();
                cout << endl;

    }

private:

    time_t mytime;
    struct tm *mytm;
    char * wDay;
    /*struct tm {
                       int tm_sec;    /// Seconds (0-60)
                       int tm_min;     // Minutes (0-59)
                       int tm_hour;   // Hours (0-23)
                       int tm_mday;   // Day of the month (1-31)
                       int tm_mon;    // Month (0-11)
                       int tm_year;   // Year - 1900
                       int tm_wday;   // Day of the week (0-6, Sunday = 0)
                       int tm_yday;   // Day in the year (0-365, 1 Jan = 0)
                       int tm_isdst;  // Daylight saving time
    };*/


};
void main(){
convData convObj(1439467747);
convData convObj2(1439469232);
cout << endl<<"First object address:" << &convObj;
cout <<endl;
convObj.afisareData();
cout << endl;
convObj2.afisareData();
}

来自 gmtime() 文档 here

Converts given time since epoch (a time_t value pointed to by time) into calendar time, expressed in Coordinated Universal Time (UTC) in the struct tm format. The result is stored in static storage and a pointer to that static storage is returned.

您使用的是同一个指针。

示例:

    struct tm *temp;
    time_t t1;
    time_t t2;

    t1 = 1439467747;
    t2 = 1439469232;

    temp = gmtime(&t1);
    struct tm tmt1;
    memcpy(&tmt1, temp, sizeof(tm));

    temp = gmtime(&t2);
    struct tm tmt2;
    memcpy(&tmt2, temp, sizeof(tm));

    cout << tmt1.tm_sec << endl;
    cout << tmt2.tm_sec << endl;