【发布时间】:2016-02-13 11:07:00
【问题描述】:
我想知道是否有可能在 C++ 中创建一个使用例如 float 的构造函数,但这个 float 不是必需的。我的意思是:
构造函数:
Fruit::Fruit(float weight)
{
weight = 1;
this->setWeight(weight);
}
我需要使用一个构造函数来做类似的事情:
Fruit pear = Fruit(5); - gives a pear with weight 5
Fruit strawberry = Fruit(); - gives a strawberry with default weight 1
【问题讨论】:
标签: c++ constructor default-value