Specification for the use of unions that include custom data types

Use of unions that include custom data types

When we run the following code:

#include <iostream>  
using namespace std;  
  
class Person  
{  
private:  
    int age;  
    float height;  
public:  
    Person(int age, float height)  
    {  
        this->age = age;  
        this->height = height;  
    }  
    Person()  
    {  
        this->age = 0;  
        this->height = 0;  
    }  
    ~Person()  
    {  
        cout << "Person类对象析构" << endl;  
    }  
    void ShowInf()  
    {  
        cout << "age:" << this->age << ";height:" << this->height << endl;  
    }  
};  
  
struct Student  
{  
    int StudNum;  
    float weight;  
    Student(int StudNum, float weight)  
    {  
        this->StudNum = StudNum;  
        this->weight = weight;  
    }  
    Student()  
    {  
        this->StudNum = 0;  
        this->weight = 0;  
    }  
    ~Student()  
    {  
        cout << "析构Student的类对象" << endl;  
    }  
    void ShowInf()  
    {  
        cout << "StudNum:" << this->StudNum << ";weight" << this->weight << endl;  
    }  
};  
  
union infor  
{  
    Person PersonObject;  
    Student StudentObject;  
  
    infor()  
    {  
        cout << "调用union的构造函数" << endl;  
    }  
    ~infor()  
    {  
        cout << "调用union的析构函数" << endl;  
    }  
};  
  
int main()  
{  
    infor M_infor;  
    M_infor.PersonObject.ShowInf();  // 访问union成员的类成员函数
    M_infor.StudentObject.ShowInf();  // 访问union成员的类成员函数
}  

 

The results are as follows:

 

Logically speaking, it should be what we expect to "construct the object of the union -> call the parameterless constructor of the Person class member in the union to initialize the Person in the union", but the facts often "backfire"!

We know that the characteristic of union is that "the members of its object share a memory area", which means that this memory area is public and can be used as any data member in the union object. "Shared area" also means "There is no difference between the data stored in this memory area, why is the custom data type initialized by the system by default? Why is the custom data type so special?" That is to say, the custom data type will be castrated at this time "The system's own parameterless constructor, destructor, and copy constructor" has become a class that only contains "custom member functions and custom member data". At this time, the class loses its custom aura, like basic The data type is the same-the object cannot be initialized by default, and the destructor class object cannot be released by default. There is no default member function given by the system.

If we need to call it after we customize it, the system will not call you automatically!

If we want to initialize the members of the union, we need to perform the following steps:

① After the union object is declared, manually call the members of the union object to initialize;

② After using the union object, you need to deconstruct the object member of the union, clear all the data of the object, and then use the space to store another union object member. This is a good programming habit, and you do not leave a tail when doing things.

After correction, the code is as follows:

#include <iostream>  
using namespace std;  
  
class Person  
{  
private:  
    int age;  
    float height;  
public:  
    void PersonInit(int age, float height)  
    {  
        this->age = age;  
        this->height = height;  
        cout << "调用Person类对象的有参初始化函数" << endl;  
    }  
    ~Person()  
    {  
        cout << "Person类对象析构" << endl;  
    }  
    void ShowInf()  
    {  
        cout << "age:" << this->age << ";height:" << this->height << endl;  
    }  
};  
  
struct Student  
{  
    int StudNum;  
    float weight;  
    void StudentInit(int StudNum, float weight)  
    {  
        this->StudNum = StudNum;  
        this->weight = weight;  
        cout << "调用Student类对象的有参初始化函数" << endl;  
    }  
    ~Student()  
    {  
        cout << "析构Student的类对象" << endl;  
    }  
    void ShowInf()  
    {  
        cout << "StudNum:" << this->StudNum << ";weight" << this->weight << endl;  
    }  
};  
  
union infor  
{  
    Person PersonObject;  
    Student StudentObject;  
  
    infor()  
    {  
        cout << "调用union的无参构造函数" << endl;  
    }  
  
    ~infor()  
    {  
        cout << "调用union的析构函数" << endl;  
    }  
};  
  
int main()  
{  
    infor M_infor;  
    M_infor.PersonObject.PersonInit(17, 187.3);  
    M_infor.PersonObject.ShowInf();  
    M_infor.PersonObject.~Person();  
    M_infor.StudentObject.StudentInit(7, 160);  
    M_infor.StudentObject.ShowInf();  
    M_infor.StudentObject.~Student();  
  
} 

 

The results are as follows:

 

Attention everyone: Why don’t I use the constructor of the class member to initialize the class member object in the union, because we have to know when the constructor of the class is used-we can only use the class when we define the class object The constructor to initialize the class object while defining the class object. Obviously, our union can be used as any union member data after it is defined. Therefore, we cannot or cannot use the constructor of the class object to initialize the class in the union. Object.

Guess you like

Origin blog.csdn.net/weixin_45590473/article/details/109386238