In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to convert base class objects into derived class objects in C++. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, and let's take a look at it.
Typically, in order to achieve polymorphism, we point the pointer or reference of the base class to the derived class object. When you need to use the unique method of the derived class object, you can achieve this by converting the base class pointer to the derived class pointer. It's always legal to do so. Perhaps in some special cases, the requirement is just the opposite, and we need to convert the base class object to a 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 first.
/ CBase.h / / # ifndef _ C_BASE_H # define _ C_BASE_H using 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
OK, let's take a look at how to convert:
/ / main.c # include # include "CBase.h" # include "CDerived.h" int main (void) {CBase base ("father"); CDerived derived ("son"); / / incorrect call. The base class CBase has no method whoAmI / / base.whoAmI (); / / calls the method specific to the derived class CDerived whoAmI derived.whoAmI (); / / incorrect conversion / / dynamic_cast (base)-> whoAmI () / / the base class is converted to a derived class, which is compiled and runs normally. Static_cast (base). WhoAmI (); return 0;}
As you can see from the above code, the method whoAmI is unique to the derived class CDerived and cannot be called by the base class object. The intention to use dynamic_cast to dynamically convert the base class object base to a derived class object will cause the compiler to report an error, because at run time, the base class object base cannot contain the properties and methods of the derived class in memory.
Why is it possible to convert statically using static_cast? This conversion statement can not be compiled in all cases. In fact, there is no conversion process at run time, we just do a small action-- take the base class object base as a reference, and construct a temporary derived class object. First review the results of the operation:
I am son! CDerived::CDerived (const CBase & base); I am father!
Then looking back at the code of the derived class CDerived, the following copy constructor is executed at run time:
CDerived (const CBase & base)
Copy the code, but unlike the default copy constructor, its argument is a reference to its base class object, so that the derived class object we constructed is in memory, and its base class part is exactly the same as base.
Inline CDerived::CDerived (const string & name): CBase (name) {NULL;}
Copy the code so we can conclude that when using static_cast for conversion, the compiler implicitly calls the copy constructor for us. It is important to note, however, that because the parameter type of the copy constructor called is different from its own type, we must write the copy constructor ourselves, and if not, the compiler will report an error because it cannot find the appropriate constructor.
These are all the contents of this article entitled "how to convert base class objects into derived class objects in C++". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.