标准列举了以下例子(3.8/7 N3797):
struct C
{
int i;
void f();
const C& operator=( const C& );
};
const C& C::operator=( const C& other)
{
if ( this != &other )
{
this->~C(); // lifetime of *this ends
new (this) C(other); // new object of type C cr
在“加速C++”的第11章中,作者提出了一个用数组模拟std::向量行为的向量类。他们使用分配程序类来处理内存管理。uncreate函数的作用是销毁数组的每个元素并释放分配给数组的空间:
template <class T> void Vec<T>::uncreate() {
if (data) {
// destroy (in reverse order) the elements that were constructed
iterator it = avail;
while (it != data)
alloc.destroy(--it
以下节目:
#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Constructor is executed\n"; }
~Test() { cout << "Destructor is executed\n"; }
};
int main()
{
Test(); // Explicit call to constructor
Test t; // local object
我试着理解在退出范围时析构函数调用的顺序。假设我有以下代码:
class Parent{
Parent(){cout<<"parent c called \n";}
~Parent(){cout<< "parent d called \n";}
};
class Child: public parent{
Child(){cout<< "child c called \n";}
~Child(){cout<<"child d called\n";}
};
现在,我知道子构造函数
我正在尝试用c++制作汤普森的构造算法(我对该语言有点陌生)。但是,在为类NFiniteAutomaton实现析构函数方面,我遇到了一些困难。在NFiniteAutomaton构造函数的某些部分中,我有:
NFiniteAutomaton() = default;
NFiniteAutomaton(std::string regex){
// A lot of code here
// ....
NFiniteAutomaton single_ele;
single_ele.init_state = new State;
single_ele.fina
我对下面的C++代码感到困惑(在在线运行它)。它结合了我在一门课程中学到的几个概念。
#include <iostream>
using namespace std;
class A {
public:
A() {cout << "A ctor" << endl;}
virtual ~A() {cout << "A dtor" << endl;}
};
class B: public A {
public:
B() {cout <
using namespace std;
class C
{
int a;
public:
C(int aa=0) {a=aa;}
~C() {cout << "Destructor C!" << a << endl;}
};
class D: public C
{
int b;
public:
D(int aa=0, int bb=0): C(aa) {b=bb;}
~D() {cout << "Destructor D!
char * buf = new char[sizeof(T)];
new (buf) T;
T * t = (T *)buf;
//code...
//here I should destruct *t but as it is argument of template and can be
//instantiated via basic types as well (say int) so such code
/*t->~T();*/
//is incorrect (maybe correct? Strange, but it works on VS 2005 for basic
在本例中链接..。
我发现了一些章节,比如,~ImprovedClass() ~Inner()
请帮帮我,他们是什么?为什么和如何在代码中使用它们?
class Inner
{
public Inner(IntPtr unkOuter)
{
}
~Inner()
{
}
}
public class Improv