Up-type conversions and down-type conversions in C + +

Source: Internet
Author: User

In the world of C + + There are two concepts, the upward type conversion, the downward type conversion, respectively describing the subclass to the base class, and the base class to the subclass of the coercion type conversion.

Force type conversions Up

Cutting: Overlay methods and sub-data loss phenomena generation cut (slice)

[CPP]View PlainCopy
  1. Class Base
  2. {
  3. Public
  4. int b;
  5. virtual void Test ()
  6. {
  7. cout << "base" <<endl;
  8. }
  9. };
  10. Class Derived: PublicBase
  11. {
  12. Public
  13. int D;
  14. virtual void Test ()
  15. {
  16. cout << "derived" <<endl;
  17. }
  18. };
  19. int main ()
  20. {
  21. Derived D;
  22. Base B = D; //Direct Assignment (generation of cutting)
  23. B.test ();
  24. base& B2 = D; //Use reference assignment (does not produce a cut)
  25. B2. Test ();
  26. base* B3 = &d; //Use pointer Assignment (does not produce cutting)
  27. B3->test ();
  28. return 1;
  29. }


Therefore, we conclude that the use of pointers and references in the upward casting process does not result in cutting, whereas the use of direct assignment results in cutting.

Forcing type conversions down

Use dynamic_cast to force type conversions down. There are a few conditions for using this keyword

1. There must be a virtual function

2. The compiler's RTTI switch must be turned on (vc6:progect-> settings-C + + TAB->category[c++ language]-> Enable RTTI)

3. Must have an inheritance relationship

[CPP]View PlainCopy
    1. Base *b = new Derived;
    2. Derived *d = dynamic_cast<derived*> (b);
    3. if (!d)
    4. {
    5. cout << "Dynamic cast err!"  <<endl;
    6. }
    7. Else
    8. {
    9. D->test ();
    10. }


In this example, the conversion succeeds if the above conditions are met. Otherwise, the Std::bad_cast exception is thrown, and the conversion returns null

Therefore, we can use dynamic_cast to determine whether there is an inheritance relationship for two classes

Up-type conversions and down-type conversions in C + +

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.