I have a question about the timing of the C++ temporary object, generator, and extinction.

Asked 2 years ago, Updated 2 years ago, 103 views

class Stock
{
private:
    string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot()
    {
        total_val = shares * share_val;
    }
public:
    //Two constructors
    Stock(); // Default constructor
    Stock(const string &co, long n=0, double pr=0.0);
    ~Stock(); // Destroyer
    void acquire(const string &co, long n, double pr);
    void buy(long num, double price);
    void sell(long num, double prive);
    void update(double price);
    void show() const;
    const Stock& topval(const Stock &s) const;
    int Setshares() const { return shares; }
    double Setshareval() { return share_val; }
    double Settotalval() { return total_val; }
    string Setcompany() { return company; }
};

int main()
{
    {
        cout<<"Create new object using constructor.\n";
        Stock stock1("NanoSmart",12,20.0);
        stock1.show();
        Stock stock2 = Stock("Boffo Objects",2,2.0);
        stock2.show();

        Replace cout<<"stock1" with stock2.\n";
        stock2=stock1;
        output cout<<"stock1 and stock2".\n";
        stock1.show();
        stock2.show();

        Reset the object using the cout<<" constructor.\n";
        stock1 = Stock ("Nifty Foods", 10,50.0); // Temporary Object
        cout<<"Updated stock1: \n";
        stock1.show();
        cout<<"Exit the program.\n";
    }
    return 0;
}

Hello? I'd like to ask you a question because I don't understand something while studying. In one example, it's about the creator, the extinction, the creation, and the extinction of temporary objects, but the output results are a little confusing. The book explains the source analysis. The following syntax for the main function is

Stock stock2 = Stock("Boffo Objects",2,2.0); // explicitly calling constructor?

The standard c++ allows the compiler to execute this syntax in two ways. The first method is to run exactly the same as Stock stock1("NanoSmart", 12,20.0); (it seems to copy the value as soon as you call the constructor) If it's wrong, please correct it.)

Another method is that the call to the constructor generates a temporary object first, and
The temporary object is copied to stock2. And then throw away the temporary object. If the compiler uses this method, the destroyer for the temporary object will be called.

We learned that temporary objects disappear when you move on to the next row like constants. But Although temporary objects are disposed of immediately, some compilers maintain temporary objects for longer than this, and in such cases, a destructor output message will be printed later. If you look at the output picture, we generate stock2 and there is no trace of the output for the destructor call, so I guessed two things about my compiler.

But then there's a problem.

stock1 = Stock("Nifty Foods",10,50.0); //Temporary Object

In the syntax, stock1 calls the constructor in the same way that it initializes stock2 as described above. The difference is that stock2 is initialization and stock1 seems to be the difference between adding to a new object.

But if you look at the output picture, you can see that the destructor call behind this phrase was made: Hello, Nifty Foods!. In this case, you can see that the destructor was called as soon as the constructor was called, the destructor was called as the temporary object was created and then disappeared as soon as it went over to the next row. It's the case of number 2

However, as you can see from the photo output, Stock stock2 = Stock ("Boffo Objects", 2, 2.0); did not immediately call the extinction.More precisely, there is no extinction call for Stock ("Boffo Objects", 2,2.0); at all

If this is the case, you might think of it as the 1st case where the temporary object is not created because you do not see the destructor call of Stock("Boffo Objects",2,2.0);

The question is, I don't know why this is happening, even though it's the same way. All I can tell from my ability is that the difference between the two is between strong initialization and strong college entrance.

If this difference is correct, the initialization can only be attributed to the initialization method of Stock stock2 = Stock ("Bofo Objects", 2, 2.0); which is the same initialization method as the first method, Stock stock1 ("NanoSmart", 12, 20.0);...How should I understand it?

c++ vs2017

2022-09-22 19:32

1 Answers

Before we begin, I would like to tell you that initialization and adversation have different properties.

First of all, there are two possible interpretations of the code below.

Stock stock2 = Stock("Boffo Objects",2,2.0);

Interpretation 1

Stock stock2(Stock("Boffo Objects",2,2.0));

Interpretation 2

Stock stock2("Boffo Objects",2,2.0);

However, most compilers behave like Analyze 2. This is the result of the compiler being optimized by the copy omitted rule, and from C++17, the standard specifies to act as Analyze 2.

In other words, when you initialize a variable with a temporary object, the Copy Skip rule does not create a temporary object, but immediately calls the constructor of the object.

By contrast, the code below is not the initialization syntax in which the variable is generated, but the syntax in which the substitution operation operates.

stock1 = Stock("Nifty Foods",10,50.0);

The temporary object Stock("Nifty Foods",10,50.0) must be created for the substitution operation, and the destructor will be called at the end of the substitution operation.

When you clean up, temporary objects may not be created by copy omission during the initialization of variables, and substitution operations do not.

For your information, the code below generates only one temporary object by omitting copy.

stock1 = Stock(Stock("Nifty Foods",10,50.0));


2022-09-22 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.