Object array initialization without default constructor

后端 未结 11 2103
误落风尘
误落风尘 2020-11-22 04:20
#include 
class Car
{
private:
  Car(){};
  int _no;
public:
  Car(int no)
  {
    _no=no;
  }
  void printNo()
  {
    std::cout<<_no<

        
11条回答
  •  无人共我
    2020-11-22 04:40

    No, there isn't. New-expression only allows default initialization or no initialization at all.

    The workaround would be to allocate raw memory buffer using operator new[] and then construct objects in that buffer using placement-new with non-default constructor.

提交回复
热议问题