C++ Suppress Automatic Initialization and Destruction

前端 未结 5 1832
予麋鹿
予麋鹿 2021-01-03 02:15

How does one suppress the automatic initialization and destruction of a type? While it is wonderful that T buffer[100] automatically initializes all the elemen

5条回答
  •  醉话见心
    2021-01-03 02:51

    This code:

    #include 
    #include 
    using namespace std;
    
    int created = 0, destroyed = 0;
    
    struct S
    {
        S()
        {
            ++created;
        }
        S(const S & s ) {
            ++created;
        }
        ~S()
        {
            ++destroyed;
        }
    };
    
    int main()
    {
        {
            std::vector vec;
            vec.reserve(50);
        }
    
        std::cout << "Created:\t"   << created   << std::endl;
        std::cout << "Destroyed:\t" << destroyed << std::endl;
        return 0;
    }
    

    has exactly the output you want - I'm not sure what your question is.

提交回复
热议问题