【发布时间】:2020-06-08 09:45:45
【问题描述】:
考虑下面的代码:
#include <iostream>
class tester
{
public:
tester(){}
explicit tester(double val) :
m_a(val) // I assume this now overwrites the "default" initialise value?
{}
double m_a {1.123}; // Default constructor value?
};
int main()
{
tester t1;
tester t2(2.456);
std::cout << "t1:" << t1.m_a << std::endl;
std::cout << "t2:" << t2.m_a << std::endl;
return 0;
}
我的问题是,你能在类和构造函数体中同时拥有初始化值吗? - 编译器如何解决这个问题?看来构造函数获胜,因为该程序的输出是:
t1:1.123
t2:2.456
【问题讨论】:
-
您显示的代码很好,并且“我假设现在这会覆盖“默认”初始化值?”是正确的。
-
谢谢 - 我将帖子链接到建议的问题,该问题也有我正在寻找的答案:)
标签: c++ constructor initialization