Avatar of pillmill
pillmill asked on

unable to use static_cast ?

I am debugging some legacy C++ code and want to change a
C-style cast to a C++ static_cast. I get syntax errors or
type Eng not found for the changed code:

Original:
Eng *      CEng(void)            {  return ( Eng * ) Engi();  }

Changed:
Eng *      CEng(void)            {  return ( static_cast (Eng *) ) Engi();  }

How can I fix this ?
C++

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jkr

trinitrotoluene

also static_cast does no runtime checking, so you need to avoid any esoteric casts.
For instance you should not cast a struct into an int or a double to a pointer.

In short do not try to subvert the existing type system

Check
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Type_Casting
evilrix

>> For instance you should not cast a struct into an int or a double to a pointer.
Um, static_cast performs compile time checks and neither of the two examples you've given would even compile!

@pillmill, What is the return value from Engi()? Is there a polymorphic relationship between this and Eng? Is this performing a down cast? If the answer to these two questions are 'yes' you might want to consider using dynamic cast, which will use RTTI to validate the cast is safe at runtime. If it's not for a pointer type cast it will return NULL rather than a pointer to an invalid object that would result in undefined behavior. You might want to consider reading this tutorial, which explains all the different casting options available to you in C++.

http://www.cplusplus.com/doc/tutorial/typecasting.html

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck