在函数调用中使用构造函数?
我一直在寻找一个很好的解释,解释为什么/为什么下面使用 struct 构造函数作为函数参数是合法的。有人可以提供一个吗?
// Begin simple illustrative example C++ program
#include<vector.h>
struct Item
{
Item(double data, const int lead)
: m_grid(data), m_lead(lead) {}
double m_grid;
int m_lead;
};
int main()
{
double img = 0.0;
int steps = 5;
std::vector<Item> images;
for (int i = 0; i < steps; i++)
{
img += 2.0;
images.push_back(Item(img,i));
}
return 0;
}
我的印象是构造函数既没有返回类型也没有声明......
I've been searching awhile for a good explanation of why/why not the following use of the struct
constructor as a function argument is legal. Can someone provide one?
// Begin simple illustrative example C++ program
#include<vector.h>
struct Item
{
Item(double data, const int lead)
: m_grid(data), m_lead(lead) {}
double m_grid;
int m_lead;
};
int main()
{
double img = 0.0;
int steps = 5;
std::vector<Item> images;
for (int i = 0; i < steps; i++)
{
img += 2.0;
images.push_back(Item(img,i));
}
return 0;
}
I was under the impression a constructor has neither a return type nor statement...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是合法的。
你永远不会自己调用构造函数;实际上,您只是声明了
Item
类型的未命名或“临时”对象。查看当您使对象未命名时语法如何演变:尽管看起来好像您像函数一样调用构造函数,但事实并非如此。
无论如何,您可以在函数参数等中使用临时值作为“右值”(因为它是一个),这就是您在这里所做的。
顺便说一句,不要使用旧的
iostream.h
和vector.h
标头。它们早于 1998 年。在 ISO 标准 C++ 中,您应该分别使用iostream
和vector
。 C++ 中的标准头文件不以“.h”结尾(inb4,忽略为向后兼容而继承的 C 头文件)。It is legal.
You never call the constructor yourself; you're actually just declaring an unnamed or "temporary" object of type
Item
. See how the syntax evolves when you make the object unnamed:Even though it looks as if you're calling the constructor like a function, you're not.
Anyway, you can use the temporary as an "rvalue" (because it is one) in function arguments and the like, which is what you're doing here.
BTW, don't use the old
iostream.h
andvector.h
headers. They predate 1998. In ISO Standard C++, you should useiostream
andvector
respectively. Standard headers in C++ do not end in ".h" (inb4, ignoring the C headers inherited for backward compatibility).传递给
push_back
的不是构造函数或其返回值。 C++实际上使用构造函数创建一个无名的临时对象,该对象仅在函数调用期间存在;通常在堆栈上。然后将其传递给push_back
,然后push_back
将其内容复制到您的向量中。It's not the constructor or its return value that's passed to
push_back
. C++ actually uses the constructor to create a nameless temporary object, which exists only for the duration of the function call; typically, on the stack. This is then passed topush_back
, andpush_back
copies its contents into your vector.这是合法的,因为
push_back
通过 const 引用获取其参数,然后创建该对象的副本。构造函数的调用创建一个临时对象,它是一个右值。 const 引用可以绑定右值。该方法不能修改它所传递的对象,但它可以创建一个副本。This is legal, because
push_back
takes it's argument by const reference, and then creates a copy of the object. The call of the constructor creates a temporary object, which is an rvalue. A const reference can bind an rvalue. The method cannot modify the object it is passed, but it can create a copy.虽然看起来像函数调用,但表达式 Item(img,i) 实际上是创建一个临时对象。不同之处在于,在运行时,将为堆栈上的对象分配内存,然后调用构造函数,而如果这是常规函数调用,则不会分配内存。
Although it looks like a function call, the expression Item(img,i) is actually the creation of a temporary object. The difference is that in runtime, memory will be allocated for the object on the stack, and then the constructor will be called, whereas if this were a regular function call no memory would be allocated.