'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:
void func(const StringClass & param1, const StringClass & param2);
And then we pass to the function for example, a C string:
func("test", test);
Where "test"
is a C string and test
is an object of our 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. 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. If our function is declared like this:
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. 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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|