在C++编程中,使用struct和class都可以定义类。
在C++11标准中,11章开头写到:
使用class定义的类的成员,默认是private的,比如你写个构造函数,如果不加public访问修饰符,则构造函数为私有,就没法用这个构造函数创建对象。
使用struct或union定义的类的成员,默认是public的。
class X {
int a; // X::a is private by default
};
struct S {
int a; // S::a is public by default
};
11.2中写到,使用class或struct来定义子类时,如果对基类不加访问修饰符,则默认的访问修饰符是不同的。?
struct默认是public,class默认是private。
class B { /_ ... _/ };
class D1 : private B { /_ ... _/ };
class D2 : public B { /_ ... _/ };
class D3 : B { /_ ... _/ }; // B private by default
struct D4 : public B { /_ ... _/ };
struct D5 : private B { /_ ... _/ };
struct D6 : B { /_ ... _/ }; // B public by default
class D7 : protected B { /_ ... _/ };
struct D8 : protected B { /_ ... _/ };
在stackoverflow中查到相关解说:
The differences between a class and a struct in C++ is:
* struct members and base classes/structs are public by default.
* class members and base classes/struts are private by default.
Both classes and structs can have a mixture of public, protected and private members, can use inheritance and can have member functions.
I would recommend you:
* use struct for plain-old-data structures without any class-like features;
* use class when you make use of features such as private or protected members, non-default constructors and operators, etc.
让我们来写点代码体验一下。
test.cpp:
#include <stdio.h>
struct A
{
int n;
void printValue(){printf("n is %d.\n", n);}
};
int main()
{
??A a;
??a.n = 1;
??a.printValue();
??return 0;
}
$ g++ -o test test.cpp
$ ./test
n is 1.
使用struct关键字定义一个类,类中包含一个成员变量和成员函数,都是public的,可以直接访问。
将上例中test.cpp的代码中的class改成struct,编译,提示有错误:
$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13:5: error: ‘int A::n’ is private within this context
???13 |???a.n = 1;
??????|?????^
test.cpp:6:5: note: declared private here
????6 | int n;
??????|?????^
test.cpp:14:16: error: ‘void A::printValue()’ is private within this context
???14 |???a.printValue();
??????|????????????????^
test.cpp:7:6: note: declared private here
????7 | void printValue(){printf("n is %d.\n", n);}
??????|??????^~~~~~~~~~
使用class关键字则成员默认变成了私有,不能直接访问。添加public访问说明,则可以正常运行。
test.cpp
#include <stdio.h>
class A
{
public:
int n;
void printValue(){printf("n is %d.\n", n);}
};
int main()
{
??A a;
??a.n = 1;
??a.printValue();
??return 0;
}
$ g++ -o test test.cpp
$ ./test
n is 1.
改为struct关键字,添加private访问修饰符,也不能直接访问:
test.cpp
#include <stdio.h>
struct A
{
private:
int n;
public:
void printValue(){printf("n is %d.\n", n);}
};
int main()
{
??A a;
??a.n = 1;
??a.printValue();
??return 0;
}
$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:14:5: error: ‘int A::n’ is private within this context
???14 |???a.n = 1;
??????|?????^
test.cpp:6:5: note: declared private here
????6 | int n;
??????|?????^
然后试一下子类访问基类的权限:
test.cpp
#include <stdio.h>
struct A
{
int n;
void printValue(){printf("n is %d.\n", n);}
};
struct B : A
{
};
class C : A
{
};
int main()
{
??B b;
??C c;
??b.n = 1;
??b.printValue();
? c.n = 1;
??c.printValue();
??return 0;
}
$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:25:5: error: ‘int A::n’ is inaccessible within this context
???25 |???c.n = 1;
??????|?????^
test.cpp:5:5: note: declared here
????5 | int n;
??????|?????^
test.cpp:26:16: error: ‘void A::printValue()’ is inaccessible within this context
???26 |???c.printValue();
??????|????????????????^
test.cpp:6:6: note: declared here
????6 | void printValue(){printf("n is %d.\n", n);}
??????|??????^~~~~~~~~~
test.cpp:26:16: error: ‘A’ is not an accessible base of ‘C’
???26 |???c.printValue();
??????|????????????????^
我们看到使用struct定义的子类,可以访问基类成员,而使用class定义的子类不可以。
再试一下Union的使用:
union.cpp
#include <stdio.h>
union U
{
struct A
{
??int n;
??void printValue(){printf("n is %d.\n", n);}
} a;
void printUnion(){printf("Struct A.n value is %d.\n", a.n);}
private:
??int b;
};
int main()
{
??U u;
??u.a.n = 1;
??u.printUnion();
??return 0;
}
$ g++ -o union union.cpp ^C
$ ./union
Struct A.n value is 1.
使用union的话,也是默认为public访问权限,如果加上了private,就没法直接访问了。
gcc版本:
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.??There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
参考:
oop - When should you use a class vs a struct in C++? - Stack Overflowhttps://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c
|