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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求助嗎,關于拷貝構造函數(shù)出錯的問題!

求助嗎,關于拷貝構造函數(shù)出錯的問題!

catspeake 2021-10-05 11:07:35
#include<iostream>using namespace std;class CAT{int * itsAge;public:CAT():itsAge(new int(5)){}CAT(const CAT& s){cout<<"copy"<<endl;int itsage;if(itsAge){*(this->itsAge) = *s.itsAge; }}~CAT(){delete itsAge;}int GetAge() const {return *itsAge;}void SetAge (int age){*itsAge=age;}};int main(){CAT frisky;cout<<"frisky's age:"<<frisky.GetAge()<<endl;cout<<"Setting frisky to 6...\n";frisky.SetAge(6);cout<<"Creating boots from frisky\n";CAT boots(frisky);cout<<"frisky's age:"<<frisky.GetAge()<<endl;cout<<"boot's age:"<<boots.GetAge()<<endl;cout<<"setting frisky to 7...\n";frisky.SetAge(7);cout<<"frisky's age:"<<frisky.GetAge()<<endl;cout<<"boots' age:"<<boots.GetAge()<<endl;}
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

復制構造函數(shù)沒有為itsAge分配空間

同樣也沒有被itsAge分配初始值

導致itsAge在執(zhí)行復制構造時為隨機值

對隨機值地址賦值 導致程序崩潰


修改為


 CAT(const CAT& s){  cout<<"copy"<<endl;  int itsage;  //if(itsAge){*(this->itsAge) = *s.itsAge; }  itsAge = new int(*s.itsAge); }


查看完整回答
反對 回復 2021-10-10
  • 1 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號