How to securely convert a base object to a derived object in C ++

Source: Internet
Author: User

Generally, to achieve polymorphism, we direct the pointer or reference of the base class to the object of the derived class. When you need to use the special method of the derived class object, you can convert the base class pointer to the pointer of the derived class for the purpose. This is always legal. In some special cases, the requirement is the opposite. We need to convert the base class object to the derived class object. Yes, it's an object, not a pointer. Let's take a look at the sample code of our base class and subclass!

//// CBase.h// #ifndef __C_BASE_H#define __C_BASE_Husing std::string;using std::cout;using std::endl;class CBase{protected :string _name;public :CBase(const string &name);virtual ~CBase(void);};inline CBase::CBase(const string &name) : _name(name){ NULL; }inline CBase::~CBase(void){ NULL; }#endif // __C_BASE_H

Okay. Let's take a look at how to convert it:

//// Main. C // # include <iostream> # include "cbase. H "# include" cderived. H "int main (void) {cbase base (" father "); cderived derived (" son "); // The base class cbase has no way to whoami // base. whoami (); // call the whoamiderived method unique to cderived. whoami (); // incorrect conversion // dynamic_cast <cderived> (base)-> whoami (); // The base class is converted to a derived class and runs properly after compilation. static_cast <cderived> (base ). whoami (); Return 0 ;}

From the code above, we can see that the method whoami is unique to the cderived derived class, and the base class object cannot call it. The compiler reports an error when using dynamic_cast to dynamically convert the base class Object base to a derived class object because the base class Object base cannot contain the attributes and methods of the derived class in the memory during runtime. Why can I use static_cast for static conversion? This conversion statement can not be compiled in any situation. In fact, the conversion process has not occurred during the runtime. We just made a small action-with the base class object as the reference, and constructed a temporary derived class object. First, review the running results:

I am son!
Cderived: cderived (const cbase & base );
I am Father!

Then, let's look back at the code of the derived class cderived. The following copy constructor is executed:

CDerived(const CBase &base);

However, unlike the default replication constructor, the parameter of the constructor is the reference of its base class object. In this way, the derived class object constructed is in the memory, the base class is exactly the same as the base class.

inline CDerived::CDerived(const string &name) : CBase(name){ NULL; }

Therefore, we can draw a conclusion that when static_cast is used for conversion, the compiler implicitly calls the replication constructor for us. However, you need to note that because the called replication constructor parameter type is different from its own type, we must compile this replication constructor in person. If not, the compiler reports an error because the appropriate constructor cannot be found.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.