对象的 C++ vector 和对析构函数的过度调用?

标签 c++ vector destructor

我想知道的是代码是否多次调用析构函数以及以这种方式编写代码是否正确。看起来创建的对象在加载到 vector 之前超出了范围,但对象并没有消失,而是停留在 vector 中,并在程序完成时再次破坏。这是输出:

object::constructor:
before push_back
object::destructor:
object::constructor:
before push_back
object::destructor:
object::destructor:
object::call(): begin
0
object::call(): end
object::call(): begin
1
object::call(): end
object::destructor:
object::destructor:

Process returned 0 (0x0)   execution time : 0.313 s
Press any key to continue.

这是main.cpp

#include <vector>
#include <iostream>
#include "object.h"

int main()
{
    int max = 2;
    std::vector <object> OBJECTS;

    for(int index = 0; index < max; index++)
    {
            object OBJECT(index);
            std::cout<<"before push_back"<<std::endl;
            OBJECTS.push_back(OBJECT);
    }

    for(int index = 0; index < max; index++)
        OBJECTS[index].call();

    return 0;
}

这是object.h

#ifndef OBJECT_H
#define OBJECT_H

#include <iostream>

class object
{
        private:

            int value;

        public:

        object(){}
        object(int value)
        {
            std::cout<<"object::constructor: "<<std::endl;
            this->value = value;
        }
        ~object()
        {
            std::cout<<"object::destructor: "<<std::endl;
        }
        void call()
        {
            std::cout<<"object::call(): begin"<<std::endl;
            std::cout<<value<<std::endl;
            std::cout<<"object::call(): end"<<std::endl;
        }
};
#endif

这是来自下面 Chowlett 答案的代码,以防网站崩溃。

#include <iostream>
#include <vector>

class object
{
        private:

            int value;

        public:

        object(){}
        object(int value)
        {
            std::cout<<"object::constructor: "<< value << std::endl;
            this->value = value;
        }
        object( const object& o )
        {
           std::cout<<"object::copy-constructor: " << o.value << std::endl;
           this->value = o.value + 10;
        }
        ~object()
        {
            std::cout<<"object::destructor: "<< value << std::endl;
        }
        void call()
        {
            std::cout<<"object::call(): begin"<<std::endl;
            std::cout<<value<<std::endl;
            std::cout<<"object::call(): end"<<std::endl;
        }
};

int main()
{
    int max = 3;
    std::vector <object> OBJECTS;

    for(int index = 0; index < max; index++)
    {
            object OBJECT(index);

            std::cout<<"before push_back: capacity="<< OBJECTS.capacity() << std::endl;            
            OBJECTS.push_back(OBJECT);
            std::cout<<"after push_back: capacity="<< OBJECTS.capacity() << std::endl;
    }

    for(int index = 0; index < max; index++)
        OBJECTS[index].call();

    return 0;
}

最佳答案

编译器为您生成了一个复制构造函数。添加一个带有一些调试输出的代码,您就可以了解您的代码在做什么:

object( const object& o )
{
   std::cout<<"object::copy-constructor: "<<std::endl;
   this->value = o.value;
}

关于对象的 C++ vector 和对析构函数的过度调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15277606/

相关文章:

c++ - QWebPage 和多线程

c++ - 多态对象的容器

Objective-C 结构数组

c++ - 过渡到使用 noexcept 隐式声明析构函数的 C++11

c++ - 单例实例释放

c# - 使用非托管 C++ .dll 从 C# .exe 调用函数

c++ - 模拟不存在的 3D 驱动程序

c++ - 二叉搜索树的析构函数

c++ - 将 vector 的一部分传递给 C++ 中的函数

c++ - 使用 vector push_back 的内存分配