显式使用 main 中的构造函数调用作为函数调用参数
我试图使用以下代码了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会调用函数
test
并传递Dependency1
类的临时对象。因为test
定义中的形式参数是对const
的引用,并且临时变量可以绑定到const
引用,因此您的代码可以正常工作。这被称为 C++ 最令人烦恼的解析。
D1
被解释为返回Dependency2
的函数,并采用一个指向返回Dependency1
的函数的指针作为参数。尝试
Dependency2 D1((Dependency1()));
并查看输出的变化。注意:添加一对额外的括号将使编译器将
(Dependency1())
视为表达式。This calls a function
test
and passes a temporary object of classDependency1
. Because the formal parameter in the definition oftest
is a reference toconst
and because temporaries can be bound toconst
references your code works.This is called C++ most vexing parse.
D1
is interpreted as a function returningDependency2
and taking an argument a pointer to function returningDependency1
.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.Dependency1() 创建一个 Dependency1 类型的临时对象,并将其传递给函数测试。
Dependency1() creates a temporary object of type Dependency1, that is passed to function test.