简体   繁体   English

如果我们用一个构造函数转换一个值,是否还需要复制构造函数来复制新创建的临时 object?

[英]If we convert a value with one constructor, will there also be copy constructor needed to copy the newly created temp object?

I am having a trouble understanding the topic, and so it might be a stupid question but I am still wondering:我无法理解该主题,因此这可能是一个愚蠢的问题,但我仍然想知道:

When we have a function, for example:当我们有一个 function 时,例如:

void func(const StringClass & param1, const StringClass & param2);

And then we pass to the function for example, a C string:然后我们传递给 function 例如,一个 C 字符串:

func("test", test);

Where "test" is a C string and test is an object of our StringClass .其中"test"是 C 字符串,而test是 StringClass 的StringClass And let's say our StringClass has defined a copy constructor and also a conversion constructor which can convert our "test" C string into a StringClass object.假设我们的StringClass定义了一个复制构造函数和一个转换构造函数,它可以将我们的"test" C 字符串转换为StringClass object。 I have tested it already and what I have seen is that, that there is only conversion happening and not copying, and for the other object there is only assignment which I understand, since we pass it by reference.我已经对其进行了测试,我所看到的是,只有转换发生而不是复制,而对于其他 object,只有我理解的分配,因为我们通过引用传递它。 If our function is declared like this:如果我们的 function 声明如下:

void func(const StringClass param1, const StringClass param2);

And we still pass previous arguments func("test", test) , then the first argument gets converted, but no copy constructor is invoked.我们仍然通过之前的 arguments func("test", test) ,然后第一个参数被转换,但没有调用复制构造函数。 And for the second parameter copy constructor is invoked.并为第二个参数调用复制构造函数。

But my question is - will it always be like that?但我的问题是——会一直这样吗? I mean, can other compiler treat it like that: convert the "test" C string into a StringClass object and then use the copy constructor to copy the temp object to param argument inside the function, or a conversion is enough since it creates a temp object anyways, so it won't differ between compilers? I mean, can other compiler treat it like that: convert the "test" C string into a StringClass object and then use the copy constructor to copy the temp object to param argument inside the function, or a conversion is enough since it creates a temp object 无论如何,所以编译器之间不会有区别吗?

As a first hint you can add a copy constructor that prints something to see if it gets called:作为第一个提示,您可以添加一个复制构造函数来打印一些内容以查看它是否被调用:

#include <iostream>

struct foo {
    template <size_t n>
    foo(const char(&str)[n]){
        std::cout << "converting constructor\n";
    }
    foo(const foo& f){
        std::cout << "copy constructor\n";
    }
};

void bar(foo){}

int main() {
    bar("asdf");
}

Output is : Output 是

converting constructor

No copy constructor is called in this example.此示例中未调用复制构造函数。 This is only a hint, because it is the output with one specific compiler and one specific C++ standard.这只是一个提示,因为它是带有一个特定编译器和一个特定 C++ 标准的 output。 However, once a foo has been created by calling the converting constructor, there is no reason to call the copy constructor.但是,一旦通过调用转换构造函数创建了foo ,就没有理由调用复制构造函数。 The string literal "asdf" is converted to a foo and thats it.字符串文字"asdf"被转换为foo就是这样。 There is no additional copy in the code, hence no compiler should create another copy.代码中没有额外的副本,因此没有编译器应该创建另一个副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM