首页 > 编程语言 >C++ Constructor And Destructor

C++ Constructor And Destructor

时间:2023-08-16 18:23:19浏览次数:42  
标签:destructors constructors C++ Destructor free Constructor print new

if you have problems with constructors and destructors, you can insert such print statements in constructors for your real classes to see that they work as intended. For larger programs, this exact kind of tracing becomes tedious, but similar techniques apply.

For example, you can determine whether you have a memory leak by seeing if the number of constructions minus the number of destructions equals zero. Forgetting to define copy constructors and copy assignments for classes that allocate memory or hold pointers to objects is a common — and easily avoidable — source of problems.

when does those happen? A good way to get a feel for that is to add print
statements to constructors, assignment operations, and destructors and then just
try.

For example:

struct X {
    int val;
    // simple test class
    void out(const string& s, int nv)
    { cerr << this << "–>" << s << ": " << val << " (" << nv << ")\n"; }
    X(){ out("X()",0); val=0; }
    // default constructor
    X(int v) { val=v; out( "X(int)",v); }
    X(const X& x){ val=x.val; out("X(X&) ",x.val); }
    // copy constructor
    X& operator=(const X& a)
    // copy assignment
    { out("X::operator=()",a.val); val=a.val; return *this; }
    ~X() { out("~X()",0); }
    // destructor
};

X glob(2); // a global variable
X copy(X a) { return a; }
X copy2(X a) { X aa = a; return aa; }
X& ref_to(X& a) { return a; }
X* make(int i) { X a(i); return new X(a); }
struct XX { X a; X b; };

int main(){
    X loc {4};    // local variable
    X loc2 {loc}; // copy construction

    loc = X{5};  // copy assignment
    loc2 = copy(loc);  // call by value and return
    loc2 = copy2(loc); 
    X loc3 {6};
    X& r = ref_to(loc); // call by reference and return
    delete make(7);
    delete make(8);
    vector<X> v(4);     // default values
    XX loc4;
    X* p = new X{9};    // an X on the free store
    delete p;
    X* pp = new X[5];  // an array of Xs on the free store
    delete[ ] pp;
}

Try executing this.

标签:destructors,constructors,C++,Destructor,free,Constructor,print,new
From: https://www.cnblogs.com/mingbai/p/17635893.html

相关文章

  • 【转载】c++调用win32API控制打印机打印
    原文:https://blog.csdn.net/cheng448208985/article/details/55510687win32实现将原始数据发送给打印机1、调用OpenPrinter()打开打印机,获取打印机句柄。2、初始化DOCINFO打印机结构体。3、调用StartDocPrinter()表明应用程序准备发送文档数据给打印机。4、调用StartPagePrin......
  • C++ Cast And Go Cast
    C++Astatic_castcanbeusedtoexplicitlyconvertbetweenrelatedpointertypes,suchasvoid*anddouble*reinterpret_castcancastbetweenunrelatedtypes,suchasintanddouble*.const_castcan“castawayconst.”Forexample:int*pi=static_ca......
  • c/c++参数入栈顺序和参数计算顺序
    如果大家细心的话应该知道c/c++语言函数参数入栈顺序为从右至左,那么为什么这样呢?来看看两个知识点:参数的计算顺序与压栈顺序。参数入栈顺序c/c++中规定了函数参数的压栈顺序是从右至左,函数调用协议会影响函数参数的入栈方式、栈内数据的清除方式、编译器函数名的修饰规则等。参......
  • C++类对象的创建方式
    1、默认构造函数创建类对象在C++中,当我们定义一个类时,如果没有明确地为类编写构造函数,编译器会为我们提供一个默认的构造函数。这就是我们所说的默认构造函数。默认构造函数没有任何参数,但是它会执行一些基本的任务,比如为类中的成员变量分配内存。2、拷贝构造函数创建类对象拷......
  • 6.1 C++ STL 序列映射容器
    Map/Multimap映射容器属于关联容器,它的每个键对应着每个值,容器的数据结构同样采用红黑树进行管理,插入的键不允许重复,但值是可以重复的,如果使用Multimap声明映射容器,则同样可以插入相同的键值。Map中的所有元素都会根据元素的键值自动排序,所有的元素都是一个Pair同时拥有实值和键......
  • 7.1 C++ STL 非变易查找算法
    C++STL中的非变易算法(Non-modifyingAlgorithms)是指那些不会修改容器内容的算法,是C++提供的一组模板函数,该系列函数不会修改原序列中的数据,而是对数据进行处理、查找、计算等操作,并通过迭代器实现了对序列元素的遍历与访问。由于迭代器与算法是解耦的,因此非变易算法可以广泛地应......
  • 8.1 C++ STL 变易拷贝算法
    C++STL中的变易算法(ModifyingAlgorithms)是指那些能够修改容器内容的算法,主要用于修改容器中的数据,例如插入、删除、替换等操作。这些算法同样定义在头文件<algorithm>中,它们允许在容器之间进行元素的复制、拷贝、移动等操作,从而可以方便地对容器进行修改和重组。主要包括以下......
  • 7.1 C++ STL 非变易查找算法
    C++STL中的非变易算法(Non-modifyingAlgorithms)是指那些不会修改容器内容的算法,是C++提供的一组模板函数,该系列函数不会修改原序列中的数据,而是对数据进行处理、查找、计算等操作,并通过迭代器实现了对序列元素的遍历与访问。由于迭代器与算法是解耦的,因此非变易算法可以广泛地应......
  • 8.1 C++ STL 变易拷贝算法
    C++STL中的变易算法(ModifyingAlgorithms)是指那些能够修改容器内容的算法,主要用于修改容器中的数据,例如插入、删除、替换等操作。这些算法同样定义在头文件<algorithm>中,它们允许在容器之间进行元素的复制、拷贝、移动等操作,从而可以方便地对容器进行修改和重组。主要包括以下......
  • 6.1 C++ STL 序列映射容器
    Map/Multimap映射容器属于关联容器,它的每个键对应着每个值,容器的数据结构同样采用红黑树进行管理,插入的键不允许重复,但值是可以重复的,如果使用Multimap声明映射容器,则同样可以插入相同的键值。Map中的所有元素都会根据元素的键值自动排序,所有的元素都是一个Pair同时拥有实值和键......