why is casting from a base class object to derived class object is unsafe?please explain with examples
c_plus_plus_for
0
Newbie Poster
Recommended Answers
Jump to Postlets say you have a base class called Animal and two derived classes called Dog and Cat. Casting a pointer of type Animal to Dog is unsafe because that might actually be type Cat. If there is something about Animal that will tell you whether it is a Dog or …
Jump to PostTo assure that the base class is pointing to the correct child class we use 'dynamic_cast' . This will do a RTTI to make sure that the base class is actually pointing to class to which it's being downcasted to.
All 5 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Agni
370
Practically a Master Poster
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Agni
370
Practically a Master Poster
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.