简体   繁体   中英

Destruction of a temporary object in C++

I have two classes: Vote and Voter .

The class Voter has a constructor that receives two strings.

The class Vote has a constructor that receives an object from the class Voter and a string.

Now, I do the following:

Voter vr6("Cyprus", "Regular");
eurovision += Vote(vr6, "USA");

Where eurovision is an object from a class where I overloaded the += operator.

From what I know, in the second line a temporary Vote object will be created.

My question is, how exactly is vr6 affected from the destruction of the temporary object?

EDIT: The definition of the Vote constructor and destructor:

Vote(Voter current_voter, string state1, string state2 = "", string state3 = "", string state4 = "", string state5 = "", string state6 = "", string state7 = "", string state8 = "", string state9 = "", string state10 = "") :
        voter(current_voter), voted_state(new string[VOTE_ARRAY_SIZE]){
        voted_state[0] = state1;
        voted_state[1] = state2;
        voted_state[2] = state3;
        voted_state[3] = state4;
        voted_state[4] = state5;
        voted_state[5] = state6;
        voted_state[6] = state7;
        voted_state[7] = state8;
        voted_state[8] = state9;
        voted_state[9] = state10;
    }
    ~Vote() {
        delete[] voted_state;
    }

Since you pass vr6 by value to Vote 's constructor, the object in vr6 itself will not depend on the Vote object's lifetime. Note, however, that with the "call by value" a temporary copy of vr6 will be created, and this copy will get deleted once the statement that calls Vote s constructor has ended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM