小编Ana*_*and的帖子

显式使用main中的构造函数调用作为函数调用参数

我试图使用以下代码了解主要工作中的显式构造函数调用.

#include<iostream>

using namespace std;

class Dependency1
{
      bool init;
public:
  Dependency1() : init(true) {
    std::cout << "Dependency1 construction"
              << std::endl;
  }
  void print() const {
    std::cout << "Dependency1 init: "
              << init << std::endl;
  }



};


class Dependency2 {
   Dependency1 d1;
public:
   Dependency2(const Dependency1& dep1): d1(dep1){
     std::cout << "Dependency2 construction ";
     print();
   }
   void print() const { d1.print(); }
};

void test( const Dependency1& dd1)
{
    cout  << " inside Test \n";
    dd1.print();
}



int main()
{

    test(Dependency1());
    Dependency2 …
Run Code Online (Sandbox Code Playgroud)

c++ constructor temporary most-vexing-parse

2
推荐指数
1
解决办法
930
查看次数

标签 统计

c++ ×1

constructor ×1

most-vexing-parse ×1

temporary ×1