第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

為什么我的 不能析構(gòu)coordinate 他析構(gòu)了circle~


#include<bits/stdc++.h>

using namespace std;

class shape{

public:

shape(){

cout<<"shape"<<endl;

}

virtual ~shape(){

cout<<"~shape"<<endl;

}

virtual double calc(){

cout<<"calc"<<endl;

return 0;

}

};

class? coordinate{

public:

coordinate(int x,int y){

cout<<"coordiante"<<endl;

? ? ? ? m_x=x,m_y=y;?

}

~coordinate(){

cout<<"~coordiante"<<endl;

}

? ? protected:

? ? int m_x,m_y;

};?

class circle:public shape

{

public:

circle(double r){

m_r=r;

cout<<"circle"<<endl;

coordinate *m_p=new coordinate(3,5);

}

virtual ~circle(){

cout<<"~circle"<<endl;

delete m_p;

m_p=NULL;

}

double calc(){

cout<<"circle->calc"<<endl;

cout<<m_r*m_r*3.14<<endl;

return 0;

}

protected:

double m_r;

coordinate *m_p;

};

int main(){

circle *p=new circle(3);

delete p;

p=NULL;

return 0;

}?


正在回答

1 回答

circle的類(lèi)定義內(nèi),構(gòu)造函數(shù)內(nèi)coordinate *m_p=new coordinate(3,5);與數(shù)據(jù)成員中coordinate *m_p;矛盾,

不妨改為如下,這樣隨著對(duì)象的解構(gòu),自動(dòng)進(jìn)行coordinate的析構(gòu)函數(shù)

class circle:public shape

{

public:

circle(double r):m_p(3,5)

{

m_r=r;

cout<<"circle"<<endl;

}

virtual ~circle(){

cout<<"~circle"<<endl;

}

protected:

double m_r;

coordinate m_p;

};


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_慕姐2217196 提問(wèn)者

你這樣說(shuō)也有點(diǎn)道理 我試了一下,按你那樣直接報(bào)錯(cuò)了 我的疑惑 是我的代碼 circle的析構(gòu)函數(shù)他都執(zhí)行了輸出~circle的指令 卻木有執(zhí)行析構(gòu)coordinate這個(gè)堆的指令
2020-03-07 回復(fù) 有任何疑惑可以回復(fù)我~
#2

qq_慕姐2217196 提問(wèn)者

我知道了,應(yīng)該 是你說(shuō)的問(wèn)題,堆實(shí)例化的時(shí)候前面不能加coordinate *,應(yīng)該 m_p=new coordinate(3,4);就行了,那個(gè)circle 的析構(gòu)函數(shù)是讀的是protected里面的m_p
2020-03-07 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
C++遠(yuǎn)征之多態(tài)篇
  • 參與學(xué)習(xí)       66226    人
  • 解答問(wèn)題       329    個(gè)

本教程將帶領(lǐng)大家體會(huì)面向?qū)ο笕筇匦灾械亩鄳B(tài)特性

進(jìn)入課程

為什么我的 不能析構(gòu)coordinate 他析構(gòu)了circle~

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)