If I have the following c++ code:
class foo{
public:
explicit foo(int i){};
};
void f(const foo &o){
}
And then I call
f(foo(1));
Is foo(1)
constructor call or function-style cast?
A constructor for a class type is called whenever a new instance of that type is created. If a cast creates a new object of that class type then a constructor is called.
Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example: template<typename T, typename U> T silly_cast(U const &u) { return T(u); }
The C-style cast consists of the type you want in parentheses, followed by the expression you want to be converted into that type, e.g. `(double)getInt()`. The function style cast works only slightly different, by stating the target type followed by the source expression in parentheses, i.e. `double(getInt())`.
They are the same thing.
It's a function-style cast that results in a constructor call, so both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With