C++中的struct和class的区别详解
1. C++的struct和class的区别
差异特性 | struct | class |
---|---|---|
成员访问范围 | 默认public | 默认private |
继承关系访问范围 | 默认public | 默认private |
{}初始化 | 1、纯数据或纯数据+普通方法的结构体支持;2、带构造函数或虚方法的结构体不支持 | 不支持 |
1.1 成员访问范围的差异
struct
struct Person {
int age;
}
Person person = new Person();
person.age = 12;
可以正常的编译和执行。
class
class Person {
int age;
}
Person person = new Person();
person.age = 12; // 编译出错,应改为public int age;
【扩展】如果不增加public关键字,又想在某个类如ClassA中能访问到这个Person类中的成员,可以通过友元类(friend class Xxx)来将Person中的private和protected的成员共享出去。
Person类可以这么编写:
struct Person {
friend class ClassA;
int age;
}
Person person = new Person();
person.age = 12;
在ClassA中,就可以访问Person中的所有成员了。
void ClassA:setAge() {
Person *person = new Person();
person->age = 12;
}
关于友元类的使用,可见c++论坛:https://cplusplus.com/forum/beginner/147733/
1.1 继承关系访问范围的差异
指的是子对象是否可访问到父对象中的成员。
struct : struct
struct SBase {
public:
int age = 0;
SBase() = default;
virtual ~SBase() = default;
};
struct Person : SBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int arGC, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
访问正常:
struct : class
class CBase {
public:
int age = 0;
CBase() = default;
virtual ~CBase() = default;
};
struct Person : CBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int argc, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
访问正常。
struct : private class
class CBase {
public:
int age = 0;
CBase() = default;
virtual ~CBase() = default;
};
struct Person : private CBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int argc, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
编译错误:不可访问。
class : class
class CBase {
public:
int age = 0;
CBase() = default;
virtual ~CBase() = default;
};
class Person : CBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int argc, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
编译错误:不可访问。
class : public class
class CBase {
public:
int age = 0;
CBase() = default;
virtual ~CBase() = default;
};
class Person : public CBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int argc, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
访问正常。
class : struct
struct SBase {
public:
int age = 0;
SBase() = default;
virtual ~SBase() = default;
};
struct Person : SBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int argc, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
访问正常。
class : public struct
struct SBase {
public:
int age = 0;
SBase() = default;
virtual ~SBase() = default;
};
struct Person : public SBase {
public:
Person() = default;
virtual ~Person() = default;
};
int main(int argc, const char **argv)
{
Person* child = new Person();
child->age = 1;
fprintf(stdout, "test: age=%d\n", child->age);
}
访问正常。
【总结】
- 1)子为class的,要想访问到父的public成员,需要加public关键字,即class: public xxx
- 2)子为struct,可加可不加public,都能访问到父类/结构体的成员
1.3 {}初始化的差异
struct – 纯数据+一般方法
struct StructA {
void send();
int a;
long b;
string str;
};
void StructA::send()
{
fprintf(stdout, "StructA: sending...\n");
}
int main(int argc, const char **argv)
{
StructA aS = {12, 34, "a test"};
aS.send();
fprintf(stdout, "StructA: a=%d, b=%ld, str=%s\n", aS.a, aS.b, aS.str.c_str());
}
可直接用{}初始化数据:
struct – 带构造函数
struct StructA {
void send();
int a;
long b;
string str;
StructA();
};
void StructA::send()
{
fprintf(stdout, "StructA: sending...\n");
}
int main(int argc, const char **argv)
{
StructA aS = {12, 34, "a test"};
aS.send();
fprintf(stdout, "StructA: a=%d, b=%ld, str=%s\n", aS.a, aS.b, aS.str.c_str());
}
编译失败:
struct – 带虚方法
struct StructA {
void virtual send();
int a;
long b;
string str;
};
void StructA::send()
{
fprintf(stdout, "StructA: sending...\n");
}
int main(int argc, const char **argv)
{
StructA aS = {12, 34, "a test"};
aS.send();
fprintf(stdout, "StructA: a=%d, b=%ld, str=%s\n", aS.a, aS.b, aS.str.c_str());
}
编译失败:
class
class ClassA {
int a;
long b;
string str;
};
int main(int argc, const char **argv)
{
ClassA cA = {12, 34, "a test"};
}
编译失败:
2. 拓展 :C和C++的struct的区别
到此这篇关于C++中的struct和class的区别详解的文章就介绍到这了,更多相关C++的struct与class内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章