小编fun*_*uns的帖子

为什么我需要一个构造函数?

#include<iostream>
using namespace std;

class A {
public:
    int i;
};

int main() {
  const A aa;  //This is wrong, I can't compile it! The implicitly-defined constructor does not initialize ‘int A::i’
}
Run Code Online (Sandbox Code Playgroud)

我用的时候

class A {
public:
  A() {}
  int i;
};
Run Code Online (Sandbox Code Playgroud)

还行吧!我可以编译它!为什么我使用隐式定义的构造函数时无法编译它?

c++ oop constructor

10
推荐指数
3
解决办法
391
查看次数

标签 统计

c++ ×1

constructor ×1

oop ×1