Constructors expand

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/dxd_123456/article/details/78021032

1, the default constructor
if the class does not define any constructor, the compiler automatically generates a no-argument constructor does not do anything, if you write a constructor, the compiler will no longer provide a default no-argument constructor, if also want to be constructor with no arguments, no need to define the display argument constructor
if not defined copy constructor, the compiler will automatically generate a copy constructor, will do the ordinary type of data replication.
Also generates a default destructor.

class Test7_2
{
    Test7_2() {}
    Test7_2(const Test7_2 &obj) {}

    ~Test7_2(){};
};

If you do not define a copy constructor, a copy function will be generated by default, but if we do not want to copy it? (Random copy constructor may have problems accessing illegal memory, will be mentioned below) how to achieve this purpose?
Quite simply, we only need to write the copy constructor private function, simply declare no need to implement

private:
    Test8_3(const Test8_3 &ibj);

So that we can achieve this purpose.

2, some of the rules constructor:
1) when the class is not defined in any of the constructor, c ++ compiler will provide a default no-argument constructor and default copy constructor
2) When class defines the copy constructor, c ++ compiler It does not provide a no-argument constructor
3) when class defines any non-copy constructor (ie: when provided with a reference class constructor or a constructor with no arguments), c ++ compiler does not provide a default constructor with no arguments function
4) the default copy constructor simply assign member variables
as long as you write a constructor, then you must use.
Summary:
1) A constructor is a special function in C ++ objects used to initialize the state
2) constructor is called automatically when an object is created
3) constructors and overloaded ordinary member functions follow the rules
4) object copy constructor to initialize properly an important guarantee
5) when necessary, must be hand-written copy constructor

3, a list of object initialization (construction and destruction of a plurality of objects)
object initialization occurs because the list
1) must do this:
If we have a class member, which itself is a class or a structure, and it is only the member with a parameter constructor, there is no default constructor. This time to be a member of this class initialization, it must call the constructor with parameters of the class members, if there is no initialization list, then he will not be able to complete the first step, it will error.

2) const class members if modified, the object must be initialized at the time, assigned to const int m, when the class member contains a const object, or when a quote, they must also be initialized by member initialization list, because the two objects to be initialized immediately after the statement, but in the constructor, to do an assignment for them, this is not allowed.

class Test9_1
{
public:
    Test9_1 (int a)
    {
        m_a = a;
        printf ("9_1 1111111111111111111构造函数....a: %d\n", a);
    }

    // 析构的顺序和构造的顺序相反,先构造的后析构
    ~Test9_1()
    {
        printf ("9_1 1111111111111111111析构函数....a: %d\n", m_a);
    }
private:
    int m_a;
};

int main()
{

    // Test9_1 a;

    return 0;
}

With the class constructor later, there is no default constructor without parameters
so this function can not be with us too Test9_1 a to create an object
this time object initialization list can be resolved in a class there is another no no-argument constructor of the class initialized object


class Test9_2
{
// 对象初始化列表,在构造函数后面加:,后面加上要初始化的对象
// 对象初始化列表要比当前类的构造函数先执行
// 对象的初始化先后顺序和 在对象初始化列表 的顺序无关,和在类中的声明先后顺序有关 
public: 
    Test9_2():m_a(10), m_b(20), m_c(30), m_ca(100)
    {

        printf ("9_2 222222222222构造函数....\n");
    }

    ~Test9_2()
    {
        printf ("9_2 222222222222构造函数....a: %d\n", m_ca);
    }
private:
    Test9_1 m_b;
    Test9_1 m_c;
    Test9_1 m_a;

    const int m_ca;
};

Plus we can initialize class members do not have a default constructor by Test9_2 t after such an initialization list.

4, calling sequence constructor and destructor
1) When a class has member variables are other classes of objects, called first constructor member variables, call the order of declaration in the same order; after calling the constructor's own class
2) destructor calling sequence corresponding constructor calls in reverse order

5, the constructor calls the constructor

class Test10_1
{
//构造函数中调用构造函数 不会达到预期的效果的
public:
    Test10_1(int a, int b)
    {
        m_a = a;
        m_b = b;

        Test10_1(a, b, 30);  // 匿名对象、临时对象
    }

    Test10_1 (int a, int b, int c)
    {
        m_a = a;
        m_b = b;
        m_c = c;
    }

    ~Test10_1()
    {
        printf ("析构*******a = %d, b = %d, c = %d\n", m_a, m_b, m_c);
    }
    void print ()
    {
        printf ("a = %d, b = %d, c = %d\n", m_a, m_b, m_c);
    }
private:
    int m_a;
    int m_b;
    int m_c;
};

int main10_1()
{
    Test10_1 a(10,20);
    a.print();

    printf ("--------------------------------\n");

    return 0;
}

After the implementation of this function, we found that did not print out the value of c, so construction can not achieve the intended purpose in the constructor. The final result was determined by the parameters provided.

Guess you like

Origin blog.csdn.net/dxd_123456/article/details/78021032