[ C++ ] — 结构体和共用体

一、C++中的结构体

1、声明

声明的过程不分配内存

struct Box{
	char name[10];
	double length;
	double width;
	double height;
};

声明的同时实例化,实例化要分配内存

struct Box{
	char name[10];
	double length;
	double width;
	double height;
} book;
2、初始化

初始化方式:

  • 初始化列表
  • 构造函数

(1) 初始化列表

struct Box{
	int length,width,height;
};
Box box={1.1 , 2.2 , 3.3};

若其中一个成员未初始化,则其后面的所有成员都不能初始化

Box box={1.1,2.2};  //合法
Box box={1.1, ,3.3};  //非法
(2) 构造函数

结构体中可以自定义构造函数析构函数

struct Box{
   char name[10];
   double length;
   double width;
   double height;
   Box(double L=0.1, double W=0.3){  //默认参数
   length=L;
   width=W;
}
}
int main()
{
    Box box(5.5);
    strcpy(box.name, "BOX1");
    cout << "name: " << box.name << endl;
    cout << "length: "<<box.length << endl;
    cout << "width: " << box.width << endl;
    return 0;
}

输出:

name: BOX1
length: 5.5
width: 0.3

3、成员函数

C++允许在结构中声明和定义成员函数,并且可用范围解析操作符定义函数。

struct Box {
    char name[20];
    double length;
    double width;
    double height;
    Box(double L = 0.1, double W = 0.3) {
        length = L;
        width = W;
    }
    void structFunc(void);
};
void Box::structFunc(void) {
    cout << "happy" << endl;
}
int main()
{
    Box box(1.0,2.0);
    box.structFunc();
    return 0;
}

输出:happy

4、结构体嵌套

struct Box {
    double height;
};

struct Item {
    int i;
    Box box;
};
int main()
{
    Item item;
    item.box.height = 0.1;
    return 0;
}

其中,height不是item的成员,box才是

item.height=1.0; //不合法
item.Box.height=1.0; //不合法

5、结构体作为函数参数

结构体可以传递给函数,一般情况下,通过值传递,这意味着要生成整个原始结构的副本,再传递给函数,但这会浪费时间来复制整个结构,所以一般通过引用来传递。结构体的成员一般是public的,所以函数可以访问和改变原始结构体的成员。如果不想让函数改变它们的值,则可以使用常量引用来传递。

struct Box {
    char name[20];
    double length;
    double width;
    double height;
    Box(double L = 0.1, double W = 0.3) {
        length = L;
        width = W;
    }
};

void func(const Box& box) {
    cout << box.length << endl;
}

int main()
{
    Box box(1.0,2.0);
    func(box);
    return 0;
}

输出:1

6、结构体作为函数返回值

struct Box {
    char name[20];
    double length;
    double width;
    double height;
    Box(double L = 0.1, double W = 0.3) {
        length = L;
        width = W;
    }
};


Box func() {
    Box box(1.0, 2.0);
    box.height = 3.0;
    return box;
}

int main()
{
    Box box1;
    box1 = func();
    cout << box1.height;
    return 0;
}

输出:3

二、结构体与类的相同之处

C++为C语言中的结构体引入了成员函数访问控制权限继承多态面向对象特性

  • 可设置成员的访问属性:punlic, protected, private
  • 可包含成员函数构造函数析构函数(C语言中结构体里不允许定义函数成员),而且结构体也可使用范围解析操作符(一般来说很少在结构体中定义成员函数)
  • 继承、多态等

三、结构体与类的不同

结构体是一种特殊形态的类

  • 结构体成员默认是public,而类成员默认是private

共用体

共用体是一种数据格式,能够存储不同类型的数据,但只能同时存储其中一种类型。

union one4all
{
	int int_val;
	long long_val;
	double double_val;
};

可以使用one4all来存储int,long或double,条件是在不同的时间进行。

one4all pail;
pail.int_val=15;  //存储int
pail.double_val=1.38; //存储double, int值丢失

pail有时可以是int,有时又可以是double。共用体最大的长度为其最大成员的长度

匿名共用体
匿名共用体没有名称,其成员将成为位于相同地址处的变量,每次只有一个成员是当前成员。

struct widget{
	char brand[20];
	int type;
	union{
		long id_num;
		char id_char[20];
	};
};
...
widget prize;
if(prize.type==1)
	cin>>prize.id_num;
else
	cin>>prize.id_char;

因为共用体是匿名的,所以id_num和id_char被视为prize的两个成员,它们地址相同,所以不需要union变量的标识。

应用
共用体常用于节省内存,当数据项使用两种或更多格式时,可节省空间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值