C + + Object slicing

Source: Internet
Author: User

What's the object slicing:

C + + in the process of converting a derived class to a base class, a part of the derived class is not received by the base class, leaving only the object of the base class size.


A slice of a value call:


#include <iostream>using namespace std;class base{public:virtual void func1 () { cout <<  "Base::func1" &NBSP;&LT;&LT;&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC2 () {cout <<  "Base::func2" &NBSP;&LT;&LT;&NBSP;ENDL;} private:int a;}; class derive :p ublic base{public:virtual void func1 () {cout <<  "Derive :: Func1 "&NBSP;&LT;&LT;&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout <<  "derive::func3" &NBSP;&LT;&LT;&NBSP;ENDL;} Virtual void func4 () {cout <<  "Derive::func4" &NBSP;&LT;&LT;&NBSP;ENDL;} private:int b;}; Typedef void (*func)   (); void printvtable (int* vtable) {cout <<  "  Virtual table address > " << VTable << endl;for  (int i = 0; vtable[i)  != 0; ++i) {printf (" %d virtual function address  :0X%x,->",  i, vtable[i]); func f =  (FUNC) vtable[i];f ();} cout << endl;} Void function (base b) {b.func1 ();} Void test1 () {base b1;derive d1;int* vtable1 =  (int*) (* (int*) &b1);int*  vtable2 =  (int*) (* (int*) &d1); Printvtable (VTABLE1); Printvtable (VTable2); function (d1);} Int main () {Test1 (); System ("Pause");}

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/B2/wKioL1dEBsPw8WpkAAAjKs-tdsk046.png "title=" 2.PNG " alt= "Wkiol1debspw8wpkaaajks-tdsk046.png"/>


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/BE/wKioL1dECJPS0QYsAAAo58KSjzc275.png "title=" 2.PNG " alt= "Wkiol1decjps0qysaaao58ksjzc275.png"/>

in the process of copying a slice occurs, when the constructor is called to initialize the vptr point to the base class of vtable, and only copy the object's base class part, so finally become a base class object.

To prevent this from happening, simply define the base class as a pure virtual function.




Passing a pointer to a derived class to the base class

#include <iostream>using namespace std;class base{public:virtual void func1 () { cout <<  "Base::func1" &NBSP;&LT;&LT;&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC2 () {cout <<  "Base::func2" &NBSP;&LT;&LT;&NBSP;ENDL;} private:int a;}; class derive :p ublic base{public:virtual void func1 () {cout <<  "Derive :: Func1 "&NBSP;&LT;&LT;&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout <<  "derive::func3" &NBSP;&LT;&LT;&NBSP;ENDL;} Virtual void func4 () {cout <<  "Derive::func4" &NBSP;&LT;&LT;&NBSP;ENDL;} private:int b;}; Typedef void (*func)   (); void printvtable (int* vtable) {cout <<  "  Virtual table address > " << VTable << endl;for  (int i = 0; vtable[i)  != 0; ++i) {printf (" %d virtual function address  :0X%x,->",  i, vtable[i]); func f =  (FUNC) vtable[i];f ();} cout << endl;} Void function (base* b) {b->func1 (); B->func2 ();} Void test1 () {base b1;derive d1;int* vtable1 =  (int*) (* (int*) &b1);int*  vtable2 =  (int*) (* (int*) &d1); Printvtable (VTABLE1); Printvtable (VTable2); function ((base*) &d1);} Int main () {Test1 (); System ("Pause");}


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/D0/wKioL1dEC0qDlPbcAAAiKsn7IZk444.png "title=" 1.PNG " alt= "Wkiol1dec0qdlpbcaaaiksn7izk444.png"/>


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/DB/wKiom1dECsDB09p4AAAtOtuJ8j4810.png "title=" 1.PNG " alt= "Wkiom1decsdb09p4aaatotuj8j4810.png"/>

Explain:

When a pointer to a derived class is passed to the base class, the size of the derived class is treated as a cumulative size, so only the size of the base class can be accessed, because the constructor is not in place, so the vptr is still a derived class.

This article is from the "Fun" blog, make sure to keep this source http://10725723.blog.51cto.com/10715723/1782556

C + + Object slicing

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.