C++ Class and Struct distinction
来源:互联网 发布:小学校园网络管理制度 编辑:程序博客网 时间:2024/06/08 04:50
1. Struct 的默认成员权限是 Public,而Class的默认成员权限是Private.
2. Struct 可以在定义的时候,直接以{}对成员变量赋初值,而Class不可以。
struct structA
{
char a;
char b;
int c;
};
structA a = {'a', 'a', 1};
struct的成员对齐问题:
#include <iostream.h>
#pragma pack(n) //定义编译器字节对齐方式 ,当n大于最大成员的size时则不起作用,仍然按自然对齐方式编译
struct example1
{
short a;
long b;
};
struct example2
{
char c;
example struct1;
short e;
};
#pragma pack () //取消自定义字节对齐方式
int main(int argc, char* argv[])
{
example2 struct2;
cout << sizeof(example1) <<endl;
cout << sizeof(example2) << endl;
cout <<(unsigned int)(&struct2.struct1) - (unsigned int)(&struct2) << endl;
return 0;
}
答案:8
16
4
分析:
默认情况下,编译器为结构体的每个成员按其自然条件分配空间,各成员按声明顺序存储,第一个成员的地址和整个结构
体的地址相同。(默认对齐是指按结构体中size最大的成员对齐)
long ---4
int ---4
short---2
a =>11- -
b =>1111
8
c =>1- - -
a =>11- -
b =>1111
e =>11- -
16
struct2中c占4字节,故地址长度为:4
- C++ Class and Struct distinction
- C# class and struct
- What are the differences between struct and class in C++?
- c ++ class和struct
- 【C++】struct和class
- 18.3 Class and struct differences
- In .net Class and Struct
- Learning_log—struct and class
- C# Class and Struct 区别
- The distinction of ProgressBar and SeekBar
- C\C++ struct && class 综述
- C/C++struct与C++struct/class关键字详解
- struct和class 在C/C++/C#
- C/C++struct与C++中的struct/class
- c++的class与struct VS c的struct
- [C#]class 与 struct 的区别
- C#struct 和class的不同
- C#struct和class的区别
- oracle远程备份数据库
- ubuntu Zone.Identifier:$DATA 文件做恶
- 如何得到JDBC Insert 语句执行后插入Oracle 数据库记录的主键
- SRM 457 Div2 500
- 交错数组与三维数组
- C++ Class and Struct distinction
- ERP为企业带来的效益
- js 中对数值的格式化问题
- 开始学习SDL
- Computer Based Social Engineering Tools: Social Engineer Toolkit (SET)
- 关于位操作
- sprintf的用法
- JSP分页1
- 正确使用prototype.js的Event.stopObserving方法