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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

您好,請(qǐng)問C++這段代碼該如何釋放堆內(nèi)存?

您好,請(qǐng)問C++這段代碼該如何釋放堆內(nèi)存?

C++ C
楊魅力 2021-10-07 11:07:51
#include "iostream"using namespace std;class simplecat{public:simplecat(int age,int weight){itsage=age; itsweight=weight;}~simplecat(){cout<<" destructor "<<endl;}int getage() const {return itsage;}int getweight() const {return itsweight;}private:int itsage;int itsweight;};simplecat & thefunction(){simplecat * p = new simplecat (5,9);cout<<p<<endl;return *p;}void main(){simplecat & rcat= thefunction();int age=rcat.getage();cout<<"rcat is "<<rcat.getage ()<<" years old"<<endl;} 書上對(duì)于釋放的方法說了三種,可是我沒看懂,幫是說下這三中方法是怎么做的。1、main函數(shù)里的第一行聲明一個(gè) simplecat 對(duì)象,然后從函數(shù) thefunction()按值返回該對(duì)象2、仍是在 thefunction() 中聲明一個(gè)自由存儲(chǔ)區(qū)的simple,但是讓函數(shù)thefunction()返回 一個(gè)指向該對(duì)象的指針。這樣使用完該對(duì)象后,調(diào)用函數(shù)可將該指針刪除3、在調(diào)用函數(shù)中聲明對(duì)象,然后按引用將它傳遞給函數(shù)thefunction()
查看完整描述

1 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

用delete就可以了 只要給出了分配內(nèi)存的首地址就可以了釋放代碼如下 如果我的回答讓你滿意的話 請(qǐng)選為滿意答案哦#include "iostream"
using namespace std;
class simplecat
{
public:
simplecat(int age,int weight){itsage=age; itsweight=weight;}
~simplecat(){cout<<" destructor "<<endl;}
int getage() const {return itsage;}
int getweight() const {return itsweight;}
private:
int itsage;
int itsweight;
};simplecat & thefunction()
{
simplecat * p = new simplecat (5,9);
cout<<p<<endl;
return *p;
}void main()
{
simplecat & rcat= thefunction();
int age=rcat.getage();cout<<"rcat is "<<rcat.getage ()<<" years old"<<endl;
delete &rcat;
}

 


查看完整回答
反對(duì) 回復(fù) 2021-10-10
  • 1 回答
  • 0 關(guān)注
  • 298 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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