包含自定义数据类型的union联合体的使用规范

包含自定义数据类型的union联合体的使用

当我们运行如下代码:

#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成员的类成员函数
}  

运行结果如下:

 

按道理来讲,应该是我们所期望的那样“构建union的对象->调用union中Person类成员的无参构造函数将union中的Person进行初始化”,但是事实往往“事与愿违”!

我们知道union的特性就在于“其对象的成员公用一段内存区”,也就是说这段内存区是公共的可以被当作union对象中任意一个数据成员来使用,“共享区域”也就意味着“这段内存区内存储的数据没有什么区别,为什么就自定义数据类型会被系统默认初始化呢?为什么自定义数据类型那么特殊呢?”,也就是说此时的自定义数据类型将会阉割“系统自带的无参构造函数,析构函数,拷贝构造函数”,变为仅仅包含“自定义成员函数与自定义成员数据”的类,此时的类失去了自定义的光环,像基本数据类型一样——不能默认初始化对象,也不能默认释放析构类对象,没有系统给予的默认成员函数。

如果我们需要我们需要自定义后自行调用,系统是不会自动给你调用滴!

如果我们想要初始化union的成员,我们需要进行以下步骤:

① 当声明union对象后,手动调用union对象成员进行初始化;

② 使用完union对象后,需要析构union的该对象成员,清空该对象的所有数据,然后在使用空间去存储另一个union对象成员,这是一个编程的好习惯,做事不留尾巴。

修正后,代码如下:

#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();  
  
} 

 

运行结果如下:

 

大家注意:为什么我这里不使用类成员的构造函数来初始化union中的类成员对象,因为我们要清楚类的构造函数是什么时候被使用的——只有当我们定义类对象时我们才可以使用类的构造函数去在定义类对象的同时初始化类对象,显然我们的union在定义后可以被当作任意一个union成员数据使用,因此我们不可以使用也使用不了类对象的构造函数初始化union中的类对象。

猜你喜欢

转载自blog.csdn.net/weixin_45590473/article/details/109386238