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

发布于 2024-10-09 04:37:37 字数 1188 浏览 0 评论 0原文

我试图使用以下代码了解 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 D1(Dependency1()); // this line does not work

    return 0;
}

正在调用函数 test,其中构造函数 Dependency1() 用作函数调用,而不是 Dependency1::Dependency1( ) 并且代码运行完美美好的。

现在,如果我使用类似的概念来创建 Dependency2 的对象 D1,它不起作用。 基于错误的理解,我似乎在这里做了错误的事情。

需要知道即使不使用范围解析,编译器如何解析 main 中的 Dependency1() 调用,以及为什么当我将其用作 Dependency2 构造函数中的参数时它不起作用,

谢谢, 阿南德

I am trying to understand how explicit constructor call in main works using the following code.

#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 D1(Dependency1()); // this line does not work

    return 0;
}

Function test is being called where constructor Dependency1() is used as a function call instead of Dependency1::Dependency1( ) and the code runs perfectly fine.

Now if I use similar concept to create an object D1 of Dependency2, it does not work.
Seems I am doing something wrong here based on wrong understanding.

Need to know how the Compiler resolves Dependency1() call in main even if scope resolution is not used and why it does not work when I use it as a parameter in constructor of Dependency2

Thanks,
Anand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-10-16 04:37:37

测试(Dependency1())

这会调用函数 test 并传递 Dependency1 类的临时对象。因为 test 定义中的形式参数是对 const 的引用,并且临时变量可以绑定到 const 引用,因此您的代码可以正常工作。

Dependency2 D1(Dependency1()); // 此行不起作用

这被称为 C++ 最令人烦恼的解析。 D1 被解释为返回 Dependency2 的函数,并采用一个指向返回 Dependency1 的函数的指针作为参数。

尝试 Dependency2 D1((Dependency1())); 并查看输出的变化。

注意:添加一对额外的括号将使编译器将 (Dependency1()) 视为表达式。

test(Dependency1())

This calls a function test and passes a temporary object of class Dependency1. Because the formal parameter in the definition of test is a reference to const and because temporaries can be bound to const references your code works.

Dependency2 D1(Dependency1()); // this line does not work

This is called C++ most vexing parse. D1 is interpreted as a function returning Dependency2 and taking an argument a pointer to function returning Dependency1.

Try Dependency2 D1((Dependency1())); and see the change in output.

Note: Putting an extra pair of parenthesis would make the compiler treat (Dependency1()) as an expression.

智商已欠费 2024-10-16 04:37:37

Dependency1() 创建一个 Dependency1 类型的临时对象,并将其传递给函数测试。

Dependency1() creates a temporary object of type Dependency1, that is passed to function test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文