C++实操 - struct和class的区别

在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 Overflowicon-default.png?t=L892https://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值