What are the rules to determine whether or not a particular static_cast will call a class’s constructor? How about c style/functional style casts?
What are the rules to determine whether or not a particular static_cast will call
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Any time a new object is created, a constructor is called. A
static_castalways results in a new, temporary object (but see comment by James McNellis) eitherimmediately, or through a call to a user defined conversion. (But in
order to have an object of the desired type to return, the user defined
conversion operator will have to call a constructor.)
When the target is a class type, C style casts and functional style
casts with a single argument are, by definition, the same as a
static_cast. If the functional style cast has zero or more than oneargument, then it will call the constructor immediately; user defined
conversion operators are not considered in this case. (And one could
question the choice of calling this a “type conversion”.)
For the record, a case where a user defined conversion operator might be
called:
In this particular case, the cast is unnecessary, and the conversion
will be made implicitly in its absence. But in all cases: implicit
conversion,
static_cast, C style cast ((A) someB) or functionalstyle cast (
A( someB )),B::operator A()will be called.)