【发布时间】:2021-03-17 08:33:02
【问题描述】:
可能是一个愚蠢的问题,如果是,请告诉我,我会尽快删除它。问题是我必须在“Kambarys”类中进行深度复制(忽略混合语言,我知道我不应该这样做)。程序在尝试第二次调用函数后终止。问题可能是我在构造函数副本中的语法,但我在任何地方都找不到正确的语法。要求之一是使用“new”在动态内存中创建 langas、durys 和 kambarys,并在 Kambarys 析构函数中删除 windows 向量和 door。感谢帮助! 要求: 在 main 方法中,使用 new 操作符创建房间 k1,为其添加门窗。编写一个可以创建正确副本的构造函数 Room (const Room & k)。在方法main中,写另一个房间k2。计算踢脚线/墙面积的长度。 执行以下步骤: k2 = * k1;删除k1;
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
class Langas{
private:
float height;
float widht;
static int countL;
public:
Langas(float h, float w){
this->height=h;
this->widht=w;
countL++;
}
~Langas(){
--countL;
}
float getHeight(){
return height;
}
float getWidht(){
return widht;
}
static int getWindowCount(){
return countL;
}
};
class Durys{
private:
float heightD;
float widhtD;
static int countD;
public:
Durys(float hD, float wD){
this->heightD=hD;
this->widhtD=wD;
countD++;
}
~Durys(){
--countD;
}
float getHeightD(){
return heightD;
}
float getWidhtD(){
return widhtD;
}
static int getDoorCount(){
return countD;
}
};
class Kambarys{
private:
float heightK;
float widhtK;
float lenghtK;
public:
vector<Langas*> windows;
Durys* door;
Kambarys(float hK, float wK, float lK){
this->heightK=hK;
this->widhtK=wK;
this->lenghtK=lK;
}
Kambarys(const Kambarys &k){
this->door=k.door;
this->windows=k.windows;
heightK=k.heightK;
widhtK=k.widhtK;
lenghtK=k.lenghtK;
}
~Kambarys(){
door=NULL;
for(int i=0; i<windows.size(); i++){
delete windows[i];
}
windows.clear();
delete door;
}
float getHeightK(){
return heightK;
}
float getWidhtK(){
return widhtK;
}
float getLenghtK(){
return lenghtK;
}
void addWindow(Langas* w){
windows.push_back(w);
}
void addDoor(Durys *d){
door=d;
}
};
float countWallPlot(Kambarys* k){
float cWPlot=(2*k->getLenghtK()*k->getHeightK())+(2*k->getWidhtK()*k->getHeightK());
for(int i=0; i<k->windows.size(); i++){
cWPlot-=((k->windows[i]->getHeight()))*(k->windows[i]->getWidht());
}
cWPlot-=((k->door->getHeightD()))*(k->door->getWidhtD());
return cWPlot;
}
float countLenght(Kambarys* k){
float floorL=(k->getLenghtK()*k->getWidhtK()*2);
floorL-=(k->door->getWidhtD());
return floorL;
}
int Langas::countL=0;
int Durys::countD=0;
int main(){
Langas *langas1=new Langas(3.4, 1.2);
Durys *durys=new Durys(3.1, 1.5);
Langas *langas2=new Langas(6.4, 1.5);
Kambarys *k=new Kambarys(30.4, 40.1, 50.1);
Kambarys *k2=k;
k->addWindow(langas1);
k->addWindow(langas2);
k->addDoor(durys);
cout<<countWallPlot(k)<<" "<<countLenght(k)<<endl;
cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
k2=k;
delete k;
cout<<countWallPlot(k2)<<" "<<countLenght(k2)<<endl;
cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
}
【问题讨论】:
-
您至少需要正确实现The Rule of Three,或者使用智能指针。
-
你为什么使用
langas1、durys和langas2的指针?你在哪里delete他们? -
你为什么要
Kambarys *k2=k;和k2=k;?这是未定义的行为:cout<<countWallPlot(k2)<<" "<<countLenght(k2)<<endl;。k2是一个悬空指针。 -
k和k2是指向同一个内存的两个指针。在delete k;之后,您将无法访问k2。 -
为什么要使用指针?避免指针。例如。
Kambarys *k=new Kambarys(30.4, 40.1, 50.1);可以是Kambarys k(30.4, 40.1, 50.1);,k->addWindow(langas1);可以是k->addWindow(3.4, 1.2);。阅读Rule of zero。