C++?б??????й????????????
???????????? ???????[ 2015/2/25 15:12:16 ] ????????C++ ???й?
???????????й?????????-???ood????
????new/delete?? array new/arrray delete???
????case 1:
???????????????????????????????????? new/delete??
???????????????鹹???????????new??????£????????????????????????????delete??
???????????????????
????1??new??delete???array new/array delete???
????2???????????new???????????new?????????delete??
?????????
????????????
????m_x = new int[10];
????...
????m_y = new CString;
????????????????
????delete m_y;
????...
????delete []m_x; // ??????????????????delete????????????????? // ????array delete
????case 2??
?????????????????????????
??????????????????????????壨??????????????????????????????????
??????????????
????1?????????????????
????????????????????棬???????ú???????????????????????????
????2??????????????????????棬????????case 1????
????3???????/?????????????棻??????????????μ?case 8??
for example:
#include <iostream>
#include <string>
// Melon : ????????;
class Melon
{
public:
Melon(char * var);
~Melon();
void print(void);
protected:
private:
char * m_variety;
};
Melon::Melon(char * var)
{
m_variety = new char[strlen(var) + 1];
strcpy(m_variety?? var);
}
Melon::~Melon()
{
delete m_variety;
}
void Melon::print()
{
std::cout << "I'm a " << m_variety << "Melon/n";
}
// Meal : ????;
class Meal
{
public:
Meal(char * var?? char * res);
~Meal();
void print(void);
protected:
private:
char * m_reastaurant; // ????
Melon * m_pMelon;
// ????2
// Melon m_Melon;
};
Meal::Meal(char * var?? char * res)
// ????2????????????????
// : m_Melon(var)
{
m_pMelon = new Melon(var);
m_reastaurant = new char[strlen(res) + 1];
strcpy(m_reastaurant?? res);
}
Meal::~Meal()
{
delete m_reastaurant;
delete m_pMelon; // ???????1??
}
void Meal::print()
{
std::cout << "I'am a Meal owned by ";
m_pMelon->print();
// ????2
//m_Melon.print();
}
int main(...)
{
cout << "case 2:/n";
Meal m1("Honeydew"?? "Four Seasons"); // ????????????;
Meal m2("Cantaloup"?? "Brook Manor Pub"); // ???? С???????;
m1.print();
m2.print();
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11