“No appropriate default constructor available”--Why is the default constructor even called?

后端 未结 2 432
广开言路
广开言路 2020-12-01 23:30

I\'ve looked at a few other questions about this, but I don\'t see why a default constructor should even be called in my case. I could just provide a default constructor, bu

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 00:28

    Member initialization occurs when the constructor begins. If you do not provide an initializer in the constructor's member initialization list, the member will be default constructed. If you want to copy constructor to be used to initialize the member cube, use the member initialization list:

    ProxyPiece::ProxyPiece(CubeGeometry& c)
      : cube(c)
    { }
    

    Everything following the colon is the initialization list. This simply says that cube should be initialized with c.

    As you had it, the cube member was first default initialized and then c was copy assigned to it.

提交回复
热议问题