zoukankan      html  css  js  c++  java
  • Copy constructors, assignment operators, and exception safe assignment

    Copy constructors, assignment operators, and exception safe assignment

    link: http://www.cplusplus.com/articles/y8hv0pDG/

    // copy constructor
    Myclass::Myclass(const Myclass& myclass)
    {
        printf("this is my class copy
    ");
        
        if(this->m_arry)
        {
            printf("42
    ");
            delete[] this->m_arry;
        }
        else
            printf("41
    ");
        
        
        m_num = myclass.m_num;
        m_arry = new int[m_num];
        
        for(int i = 0; i < m_num; i++)
        {
            printf("40
    ");
            m_arry[i] = myclass.m_arry[i];
        }
    }

    Error: 这是我犯的第一错误: 这是一个构造函数, 而我在一开始还没复制之前就已经delete 这属于delete一个无效的指针。构造函数 对申请的内存进行初始化。

    Overview of C++ Variable Initialization

    https://greek0.net/cpp/initialization.html

    1. destructor:

    Like constructors, destructors have specific naming rules:
    1) The destructor must have the same name as the class, preceded by a tilde (~).
    2) The destructor can not take arguments.
    3) The destructor has no return type.

    RAII

    RAII (Resource Acquisition Is Initialization) is a programming technique whereby resource use is tied to the lifetime of objects with automatic duration (e.g. non-dynamically allocated objects). In C++, RAII is implemented via classes with constructors and destructors. A resource (such as memory, a file or database handle, etc…) is typically acquired in the object’s constructor (though it can be acquired after the object is created if that makes sense). That resource can then be used while the object is alive. The resource is released in the destructor, when the object is destroyed. The primary advantage of RAII is that it helps prevent resource leaks (e.g. memory not being deallocated) as all resource-holding objects are cleaned up automatically.

    Under the RAII paradigm, objects holding resources should not be dynamically allocated. This is because destructors are only called when an object is destroyed. For objects allocated on the stack, this happens automatically when the object goes out of scope, so there’s no need to worry about a resource eventually getting cleaned up. However, for dynamically allocated objects, the user is responsible for deletion -- if the user forgets to do that, then the destructor will not be called, and the memory for both the class object and the resource being managed will be leaked!

    The IntArray class at the top of this lesson is an example of a class that implements RAII -- allocation in the constructor, deallocation in the destructor. std::string and std::vector are examples of classes in the standard library that follow RAII -- dynamic memory is acquired on initialization, and cleaned up automatically on destruction.

    Rule: If your class dynamically allocates memory, use the RAII paradigm, and don’t allocate objects of your class dynamically

    A warning about the exit() function

    Note that if you use the exit() function, your program will terminate and no destructors will be called. Be wary if you’re relying on your destructors to do necessary cleanup work (e.g. write something to a log file or database before exiting).

    2. The hidden *this pointer

    Putting it all together:
    1) When we call simple.setID(2), the compiler actually calls setID(&simple, 2).
    2) Inside setID(), the *this pointer holds the address of object simple.
    3) Any member variables inside setID() are prefixed with “this->”. So when we say m_id = id, the compiler is actually executing this->m_id = id, which in this case updates simple.m_id to id.

    Note that our constructor is taking a parameter of the same name as a member variable. In this case, “data” refers to the parameter, and “this->data” refers to the member variable. Although this is acceptable coding practice, we find using the “m_” prefix on all member variable names provides a better solution by preventing duplicate names altogether! Recommendation: Do not add this-> to all uses of your class members. Only do so when you have a specific reason to.

    3. c++ Defining member function inside or outside the class definition 

    Outside the Class: Defining a member function outside a class requires the function declaration (function prototype) to be provided inside the class definition. The member function is declared inside the class like a normal function. This declaration informs the compiler that the function is a member of the class and that it has been defined outside the class. After a member function is declared inside the class, it must be defined (outside the class) in the program.

    Inside the Class: A member function of a class can also be defined inside the class. However, when a member function is defined inside the class, the class name and the scope resolution operator are not specified in the function header. Moreover, the member functions defined inside a class definition are by default inline functions.

  • 相关阅读:
    2013国内IT行业薪资对照表【技术岗位】
    Eclipse查看子类
    whereis 查找命令全路径
    开张了
    Ruby1.8中单行字符串写在多行
    FEMTO是什么
    FUSE文件系统
    魔兽私服pvpgn搭建
    linux网络源码分析(1)
    freehosting申请空间和ssh D设置
  • 原文地址:https://www.cnblogs.com/yetanghanCpp/p/9050080.html
Copyright © 2011-2022 走看看