我最近在工作中遇到了这个问题.我正在使用的库使用引用计数对象并实现自己的处理方式.部分实现是库的每个类都有一个私有析构函数.我猜测这是为了防止在库自动管理对象生存期时在堆栈上创建对象(它是一个场景图).
无论如何,我想在堆上分配这样一个类的数组并遇到以下问题:
#include <iostream>
using namespace std;
class test
{
public:
test() {
cout << "ctor" << endl;
}
//~test() = delete; also doesnt work
private:
~test()
{
cout << "dtor" << endl;
}
};
int main()
{
//works
auto oneInstance = new test;
//doesnt work
auto manyInstances = new test[3];
}
Run Code Online (Sandbox Code Playgroud)
数组分配使用GCC产生以下错误:
source_file.cpp: In function ‘int main()’:
source_file.cpp:15:5: error: ‘test::~test()’ is private
~test()
^
source_file.cpp:26:36: error: within this context
auto manyInstances = new test[3];
^
Run Code Online (Sandbox Code Playgroud)
为什么析构函数需要公共/可用才能在堆上分配此类的数组?当只分配像之前一行中的单个实例时,它工作正常.我也尝试使用更现代的"删除"语法,但它产生了相同的结果.
新的[]运算符中是否有任何我不知道的魔法?
编辑:
感谢您的快速帮助.我想知道为什么这段代码没有打印"dtor"两次但是: …
为什么在此代码中不调用析构函数?
#include <boost/scoped_ptr.hpp>
#include <iostream>
class MyClass {
boost::scoped_ptr<int> ptr;
public:
MyClass() : ptr(new int) { *ptr = 0; throw; std::cout<<"MyClass Allocated\n"; }
~MyClass() { std::cout<<"MyClass De-allocated\n"; }
int increment() { return ++*ptr; }
};
int main()
{
boost::scoped_ptr<MyClass> myinst(new MyClass);
std::cout << myinst->increment() << '\n';
std::cout << myinst->increment() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
编辑
从答案中,了解当构造函数中发生异常时,不会调用析构函数.但是如果异常发生在main()中,即在完全实例化MyClass对象之后,是否会调用MyClass析构函数?如果没有,那为什么它是一个智能指针?
添加代码
#include <boost/scoped_ptr.hpp>
#include <iostream>
class MyClass {
boost::scoped_ptr<int> ptr;
public:
MyClass() : ptr(new int) { *ptr = 0; std::cout<<"MyClass Allocated\n"; }
~MyClass() { std::cout<<"MyClass De-allocated\n"; …Run Code Online (Sandbox Code Playgroud) 如果对象的构造函数抛出异常,那么析构函数会被调用吗?或者这是未定义的行为?(这就是为什么我不愿意说出我的编译器的作用。)
struct foo()
{
foo(){
throw "bar";
}
~foo(){
/*am I called*/
}
};
foo f;
Run Code Online (Sandbox Code Playgroud) #include <iostream>
class A
{
public:
A()
{
std::cout << "A()" << std::endl;
}
virtual ~A()
{
std::cout << "~A()" << std::endl;
}
};
class B:public A
{
public:
B()
{
throw std::exception();
std::cout << "B()" << std::endl;
}
~B()
{
std::cout << "~B()" << std::endl;
}
};
int main()
{
try
{
B b;
}
catch(std::exception & e)
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码输出,
A()
~A()
Run Code Online (Sandbox Code Playgroud)
抛出异常时,已创建B.那么为什么B的析构函数不被称为?
我编写了一个小程序,以检查异常情况下创建shared_ptrvia new和make_shared()函数之间的区别。我到处都读到,通过make_shared()它是异常安全的。
但是,这两种情况的有趣之处在于,两种情况下的析构函数都不会在堆栈展开后调用?我错过了什么吗?提前致谢。
#include <iostream>
#include <memory>
class Car
{
public:
Car() { cout << "Car constructor!" << endl; throw std::runtime_error("Oops"); }
~Car() { cout << "Car destructor!" << endl; }
};
void doProcessing()
{
// std::shared_ptr<Car> sp(new Car());
std::shared_ptr<Car> sp2 = std::make_shared<Car>();
}
int main()
{
try
{
doProcessing();
}
catch(...)
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×5
exception ×2
boost ×1
destructor ×1
heap ×1
inheritance ×1
private ×1
scoped-ptr ×1
shared-ptr ×1