欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

    • <bdo id='hqhqu58t'></bdo><ul id='mpuf8bll'></ul>
    1. <small id='svei2xxv'></small><noframes id='r1iyv7bf'>

      <tfoot id='z5cppt08'></tfoot>

        <i id='h28xzcfe'><tr id='m6hmho63'><dt id='8f0pg60a'><q id='zcc70j6m'><span id='66qtr0w8'><b id='c689mchk'><form id='i6h52vc7'><ins id='ir7i3a21'></ins><ul id='ppnv0tsh'></ul><sub id='b40avyaa'></sub></form><legend id='psxvfl7v'></legend><bdo id='z2nqh0so'><pre id='7vfteyx4'><center id='l07aryys'></center></pre></bdo></b><th id='ocp3lwbv'></th></span></q></dt></tr></i><div class="c8jzdxauzz" id='n5rvtewi'><tfoot id='m1s379di'></tfoot><dl id='2qnfydzr'><fieldset id='cr8wsepb'></fieldset></dl></div>
      1. <legend id='cn01x0v0'><style id='ydqvrait'><dir id='wftqz7s6'><q id='23i842tc'></q></dir></style></legend>
        歡迎來到入門教程網(wǎng)!

        C++

        當(dāng)前位置:主頁 > 軟件編程 > C++ >

        &amp;lt;三&amp;gt;對象的淺拷貝和深拷貝問題

        來源:本站原創(chuàng)|時間:2023-04-02|欄目:C++|點擊: 次

        先看示例代碼

        點擊查看代碼
        #include <iostream>
         #include<cstring>
        using namespace std;
        
        class Student{
        
        public:
        
            Student(int _age , const char * _name)
            {
                 
                 this->age=_age;
        
                 int len=strlen(_name)+1;
        
                 char *tep=new char[len];
        
                 this->pName=tep;
        
                 strcpy(this->pName,_name);
        
            }
        
            ~Student(){
                
                delete[]this->pName;
                this->pName=nullptr;
               
            }
        
            void showStudent(){
                cout<<this->pName<<" "<<this->age<<endl;
            }
        
         private:
            int  age;
            char *pName;  
        };
        
        
        int main(){
        
           Student s1(20,"zhangsan");
           s1.showStudent();
        
           Student s2=s1;
          
           s2.showStudent();
        
           return 1;
        }
        
        

        上面示例代碼中,對象的默認拷貝方式是內(nèi)存數(shù)據(jù)拷貝,如果對象占用了外部資源,那么就會出現(xiàn)問題了,這里的外部資源
        就是在堆上申請的空間存放名字用,
        s1,s2兩個對象中的名字指針都是指向了同一塊堆內(nèi)存區(qū)域,在結(jié)束main函數(shù)的是,s2先析構(gòu),s2會析構(gòu)調(diào)堆上的內(nèi)存空間,
        s1再析構(gòu),由于s1對象的pName 指針和s2對象的pName指針指向的是同一塊堆內(nèi)存區(qū)域,但是該區(qū)域內(nèi)存已經(jīng)被釋放了,所以遇到了問題

        鑒于上面的問題,我們引出了 淺拷貝,深拷貝的問題和解決方法.

        Student s2=s1; 會調(diào)用拷貝構(gòu)造函數(shù)(該拷貝構(gòu)造函數(shù)可以是系統(tǒng)自動生成的,或者你自己定義一個拷貝構(gòu)造函數(shù))
        系統(tǒng)自動生成的拷貝構(gòu)造函數(shù)做的是內(nèi)存數(shù)據(jù)拷貝,所以就出現(xiàn)了上面的問題.因此我們需要定義屬于自己的拷貝構(gòu)造函數(shù)

        改造代碼如下

        點擊查看代碼
        class Student2{
        
        public:
        
            Student2(int _age , const char * _name)
            {        
                 this->age=_age;
                 int len=strlen(_name)+1;
                 char *tep=new char[len];   
                 this->pName=tep;
                 strcpy(this->pName,_name);
                 cout<<"執(zhí)行構(gòu)造函數(shù)"<<endl;
            }
        
            //自定義拷貝構(gòu)造函數(shù)
            Student2(const Student2 &_stu2){
        
                this->age=_stu2.age;    
                int len =strlen(_stu2.getPName())+1;
                char *newPName =new char[len];
                this->pName=newPName;
                strcpy(this->pName,_stu2.getPName());
                cout<<"執(zhí)行拷貝構(gòu)造函數(shù)"<<endl;
        
            }
        
            ~Student2(){
                if(this->pName!=nullptr){
                    delete[]this->pName;
                    this->pName=nullptr;
                }
            }
        
            void showStudent(){
        
                int *p=(int *)this->pName;
                cout<<this->pName<<" "<<this->age<<" pName Heap Address ="<<p<<endl;
        
            }
        
            const char * getPName() const{
                 return this->pName;
            }
        
         private:
            int  age;
            char *pName;  
        
        };
        
        
        
        
        int main(){
        
           Student2 s1(20,"zhangsan");
           
           s1.showStudent();
        
           Student2 s2=s1;
        
           s2.showStudent(); 
        
        
           return 1;
        }
        

        上面的代碼還存在一個問題如下,如果在main函數(shù)中是下面的代碼

        Student2 s1(20,"zhangsan");

        Student2 s2(30,"lisi");

        s2=s1;

        在main函數(shù)結(jié)束的是同樣會遇到問題,這個時候就引出 賦值函數(shù),我們需要定義自己的賦值函數(shù)

        在 s2=s1這一段代碼其實是這樣調(diào)用的
        s2.operator=(s1)

        在你不定義自己的賦值函數(shù)的時候,系統(tǒng)會幫我們生成一個賦值函數(shù),該賦值函數(shù)的賦值方式就是內(nèi)存的數(shù)值拷貝,這種方式
        在Student對象,會有問題,所以我們需要向自定義拷貝構(gòu)造一樣,定義一個屬于自己的賦值函數(shù)

        代碼如下

        點擊查看代碼
        class Student3{
        
        public:
        
            Student3(int _age , const char * _name)
            {        
                 this->age=_age;
                 int len=strlen(_name)+1;
                 char *tep=new char[len];   
                 this->pName=tep;
                 strcpy(this->pName,_name);
                 cout<<"執(zhí)行構(gòu)造函數(shù)"<<endl;
            }
        
            //自定義拷貝構(gòu)造函數(shù)
            Student3(const Student3 & _stu){
        
                this->age=_stu.age;    
                int len =strlen(_stu.getPName())+1;
                char *newPName =new char[len];
                this->pName=newPName;
                strcpy(this->pName,_stu.getPName());
                cout<<"執(zhí)行拷貝構(gòu)造函數(shù)"<<endl;
        
            }
        
            //自定義賦值函數(shù)
            Student3 & operator= (const Student3 & _stu){
        
                //防止自賦值
                if(this==&_stu){return *this;}
        
                //注意: 需要先釋放當(dāng)前的堆內(nèi)存空間??!
                delete []this->pName;
        
                this->age=_stu.age;    
                int len =strlen(_stu.getPName())+1;
                char *newPName =new char[len];
                this->pName=newPName;
                strcpy(this->pName,_stu.getPName());
                cout<<"執(zhí)行賦值函數(shù)"<<endl;
                return *this;
            }
        
            ~Student3(){
                if(this->pName!=nullptr){
                    delete[]this->pName;
                    this->pName=nullptr;
                }
            }
        
            void showStudent(){
        
                int *p=(int *)this->pName;
                cout<<this->pName<<" "<<this->age<<" pName Heap Address ="<<p<<endl;
        
            }
        
            const char * getPName() const{
                 return this->pName;
            }
        
         private:
            int  age;
            char *pName;  
        
        };
        
        int main(){
        
           Student3 s3(20,"zhangsan");
           
           s3.showStudent();
        
           Student3 s4(30,"lisi");
        
           s4=s3;
           
           s4.showStudent();
        
           return 1;
        }
        
        

        運用深拷貝淺拷貝實現(xiàn)String 代碼如下

        點擊查看代碼
        #include <iostream>
        #include <cstring>
        using namespace std;
        
        class MyString{
        
        public:
        
             //構(gòu)造函數(shù)
             MyString(const char * src){
        
                //創(chuàng)建空串
                if(src==nullptr){
                    this->pchar=new char[1];
                    this->pchar[0]='\0';
                }
                else{
                    int len =strlen(src)+1;
                    this->pchar=new char[len];
                    strcpy(this->pchar,src);
                }
                cout<<"執(zhí)行構(gòu)造函數(shù), 堆內(nèi)存空間地址"<<(int *)this->pchar <<endl;
        
             }
        
             //拷貝構(gòu)造
             MyString(const MyString & myString){
              
                //重新分配內(nèi)存空間 
                int len =myString.stringLen()+1;
                this->pchar=new char[len];
                strcpy(this->pchar,myString.pchar);  
                cout<<"執(zhí)行拷貝構(gòu)造函數(shù), 新創(chuàng)建堆內(nèi)存空間地址"<<(int *)this->pchar <<endl;
         
             }
        
             //賦值函數(shù)
             MyString & operator=(const MyString & myString){
        
                 //防止自賦值
                 if(this==&myString){return *this;}
                 //釋放現(xiàn)有的堆內(nèi)存空間
                 delete[]this->pchar;
                 this->pchar=nullptr;
        
                int len =myString.stringLen()+1;
        
                this->pchar=new char[len];
                strcpy(this->pchar,myString.pchar);
                
                cout<<"執(zhí)行賦值函數(shù), 新創(chuàng)建堆內(nèi)存空間地址"<<(int *)this->pchar <<endl;
        
                return *this;
        
             }
        
             int stringLen() const {     
                 return strlen(this->pchar);
             }
            
             //析構(gòu)
             ~MyString(){
                 delete [] this->pchar;
                 this->pchar=nullptr;
             }
        
             void printChar() const{      
                 cout<<this->pchar<<endl;
             }
        
        private:
            char *pchar;
           
        
        };
        
        
        int main(){
        
            MyString s1("abcd");
            s1.printChar();
        
            MyString s2=s1;//執(zhí)行拷貝構(gòu)造
            s2.printChar();
        
            MyString s3("1234");
            s3=s1;//執(zhí)行賦值函數(shù)
            s3.printChar();
        
            
            return 1;
        }
        
        

        <i id='1buarxpi'><tr id='3gmoyauq'><dt id='580dxx3s'><q id='brj98rzc'><span id='6g7yyrhx'><b id='g5ga7rua'><form id='i0lhxr4i'><ins id='gls6oh32'></ins><ul id='u6xxjcj8'></ul><sub id='epf5kxwo'></sub></form><legend id='ftmzg1b5'></legend><bdo id='6hlo0ul7'><pre id='ao2xxioc'><center id='mbj9jxrt'></center></pre></bdo></b><th id='v8u2x1g8'></th></span></q></dt></tr></i><div class="c8jzdxauzz" id='5dmkjsl4'><tfoot id='essle16i'></tfoot><dl id='ly6l5xs5'><fieldset id='g0hap4ji'></fieldset></dl></div>

        1. <legend id='oq9a1mmd'><style id='t8yt5fn3'><dir id='sghpvbpy'><q id='qucesq0v'></q></dir></style></legend>
            <bdo id='ufijuyaa'></bdo><ul id='m4dlnpzc'></ul>

              <tfoot id='f0zo4vrc'></tfoot>

              <small id='bkuq2o05'></small><noframes id='drbxu2ba'>

                <tbody id='q51r4rho'></tbody>
                • 上一篇:&amp;lt;二&amp;gt;掌握構(gòu)造函數(shù)和析構(gòu)函數(shù)

                  欄    目:C++

                  下一篇:&amp;lt;四&amp;gt;構(gòu)造函數(shù)初始化列表

                  本文標(biāo)題:&amp;lt;三&amp;gt;對象的淺拷貝和深拷貝問題

                  本文地址:http://mengdiqiu.com.cn/a1/c++/17092.html

                  網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

                  如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

                  聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

                  Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有

                    1. <small id='y8sh634l'></small><noframes id='y8v1gbss'>

                      <legend id='48rkkizk'><style id='zbi2bi4y'><dir id='q0t5k4i9'><q id='rkq8vjzk'></q></dir></style></legend>
                        <bdo id='w68beefn'></bdo><ul id='eb6yeiw1'></ul>
                      <tfoot id='nmdxocn5'></tfoot>

                      <i id='aldiyd2m'><tr id='a4mfrjpj'><dt id='fw8jnyo9'><q id='agxt6nmr'><span id='qahmulyx'><b id='dbny4zp8'><form id='4p42r1ws'><ins id='b8s1fjjx'></ins><ul id='gxpcqwf4'></ul><sub id='80c167le'></sub></form><legend id='9p8av9cq'></legend><bdo id='4a15y0t1'><pre id='w6cpyrng'><center id='2e1m0yhp'></center></pre></bdo></b><th id='rz6i6ane'></th></span></q></dt></tr></i><div class="c8jzdxauzz" id='7053lgk0'><tfoot id='e8ipwhja'></tfoot><dl id='jel8vfjs'><fieldset id='larfgyv0'></fieldset></dl></div>