type conversions

The book I'm learning C++ with briefly (as in a couple pages) talks about using the
typeName (value) // converts value to typeName type
to convert values. I understand that. Then, in the next section it briefly talks about static_cast<>. You use it to convert values from one numeric type to another. I get all that. What I don't get is why can't you just use typeName (value), why do you need the static_cast method.

I'm only on the 4th chapter in this book, and it mentions that Chapter 15 talks a lot more about these things and the other type cast operators. Should I be worried about this now, or just wait until I have read chapter 15. The book is C++ Primer Plus
Last edited on
There is a fundemental different between the two.

typeName(value) creates a new object of the new type using value as its initialiser.

But static_cast<typename>(value) treats value AS IF it was of type typeName without actually converting it.

So, typeName(value) creates a variable (basically) of the type you specify and assigns it the value (which is in parenthesis). Static_cast<typename> does not convert the original value or anything, but just makes it be treated like the type you specify. I'm not sure if I'm understanding it. I haven't studied OOP yet, if it helps.
Topic archived. No new replies allowed.