我很难弄清楚如何解决以下问题:
#include "stdafx.h"
using namespace std;
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
class Talker
{
public:
virtual void SaySomething() = 0;
};
class SoundProducer
{
protected:
string soundName;
public:
SoundProducer(string nameOfSound)
{
this->soundName = nameOfSound;
}
virtual void MakeSound() = 0;
string GetSoundName()
{
return this->soundName;
}
};
class Uppgift1 : public Talker
{
private:
SoundProducer* t;
public:
Uppgift1(){};
void SetSoundProducer(SoundProducer* _soundProducer)
{
t = _soundProducer;
}
void SaySomething()
{
t->MakeSound();
}
virtual ~Uppgift1()
{
delete t;
}
};
class Whisperer : public SoundProducer
{
public:
Whisperer() : SoundProducer("Whisper"){}
virtual void MakeSound()
{
cout << soundName << ": Ssch,hush,hush" << endl;
}
virtual ~Whisperer()
{
cout << "Whisperer destructor called" << endl;
}
};
class Shouter : public SoundProducer
{
public:
Shouter() : SoundProducer("Shout"){}
virtual void MakeSound()
{
cout << soundName << ": WOW YEEEH!!" << endl;
}
virtual ~Shouter()
{
cout << "Shouter destructor called" << endl;
}
};
int main()
{
Uppgift1 uppg1;
uppg1.SetSoundProducer(new Whisperer);
uppg1.SaySomething();
uppg1.SetSoundProducer(new Shouter);
uppg1.SaySomething();
_CrtDumpMemoryLeaks();
return 0;
}
我遇到的问题是内存泄漏,并且我试图找出在超出范围时如何删除指向类的指针。
至于现在,喊叫和低语者不会被删除,这会导致内存泄漏。
看起来唯一的方法是使SoundProducer类继承uppgift1类并将析构函数虚拟化,对吗?我有点累了,如果也让您感到困惑,也很抱歉:)
您将声音产生器设置了两次,但是UppGift只能容纳一个...。因此,将调用Shouter的析构函数,因为它是最后添加的元素,并且从未调用过Whsperer的析构函数,因为您已经将其再次设置为Shouter。
前:
Uppgift1 uppg1;
uppg1.SetSoundProducer(new Whisperer);
uppg1.SaySomething();
uppg1.SetSoundProducer(new Shouter);
您两次拨打电话,但只能保留一个。
SoundProducer* t;
后:
如果要添加多个SoundProducer,请维护它们的列表/矢量。
vector<SoundProducer*> _list;
virtual ~Uppgift1()
{
// delete t;
std::vector< SoundProducer*>::iterator it = _list.begin();
//delete *it;
for ( ; it != _list.end(); it++ )
{
delete *it;
}
_list.clear();
在继承树中,如果您尝试通过基指针删除派生类实例,则基类析构函数应该是虚拟的。这样,当您对指向派生类实例I的基类指针进行删除时,派生类析构函数将被调用现在已经编辑了代码。
#include <iostream>
using namespace std;
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <vector>
//#include <crtdbg.h>
class Talker
{
public:
virtual void SaySomething() = 0;
virtual ~Talker(){cout<<"Talker Destructor"<<endl;}
};
class SoundProducer
{
protected:
string soundName;
public:
SoundProducer(string nameOfSound)
{
this->soundName = nameOfSound;
}
virtual void MakeSound() = 0;
string GetSoundName()
{
return this->soundName;
}
virtual ~SoundProducer(){cout<<"SoundProducer Detructor"<<endl;}
};
class Uppgift1 : public Talker
{
private:
SoundProducer* t;
vector<SoundProducer*> _list;
public:
Uppgift1(){};
void SetSoundProducer(SoundProducer* _soundProducer)
{
if(_soundProducer)
{ cout<<"pushing into list"<<endl;
_list.push_back(_soundProducer);
}
// t = _soundProducer;
}
void SaySomething()
{
// t->MakeSound();
}
virtual ~Uppgift1()
{
// delete t;
std::vector< SoundProducer*>::iterator it = _list.begin();
//delete *it;
for ( ; it != _list.end(); it++ )
{
delete *it;
}
_list.clear();
}
};
class Whisperer : public SoundProducer
{
public:
Whisperer() : SoundProducer("Whisper"){}
virtual void MakeSound()
{
cout << soundName << ": Ssch,hush,hush" << endl;
}
virtual ~Whisperer()
{
cout << "Whisperer destructor called" << endl;
}
};
class Shouter : public SoundProducer
{
public:
Shouter() : SoundProducer("Shout"){}
virtual void MakeSound()
{
cout << soundName << ": WOW YEEEH!!" << endl;
}
virtual ~Shouter()
{
cout << "Shouter destructor called" << endl;
}
};
int main()
{
Uppgift1 uppg1;
SoundProducer* ptr=new Whisperer;
uppg1.SetSoundProducer(dynamic_cast<SoundProducer*>(ptr));
uppg1.SaySomething();
uppg1.SetSoundProducer(new Shouter);
uppg1.SaySomething();
// _CrtDumpMemoryLeaks();
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句