#include <iostream>
using namespace std;
#include <stdio.h>

struct _test{
int a;
_test()
:a(1){
this->a=2;
}
};

int main() {
struct _test test;
printf("struct test.a=%d\n",test.a);
return 0;
}

可能这不是特殊用法,我也是刚开始深入C++,上面的struct _test 在构造函数中给a赋值,有两种操作,一种是构造函数后的" :a(1)",意思是a=1;a猛地一看还以为函数或继承呢。另一种操作是很普通的在构造函数中赋值,两种同时存在,那么当然是构造函数中的为准了。

不知道这种 赋值方法叫什么,感觉怪异,但是也就是这么回事。

此方法如果将struct 换成class ,需要改动的一点就是添加 public:。因为class默认函数和属性为private,而struct默认为public。


 

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
猜你喜欢
  • 2021-07-17
  • 2021-10-10
  • 2021-12-09
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案