求大神帮忙看看关于这个类的构造函数该怎么改。
class WarSystem
{
public:
WarSystem();
void PrintSoldiers();
void startWar();
void startRound();
public:
Soldier pGroupA[3];
Soldier pGroupB[3];
};
WarSystem::WarSystem()
{
Soldier A1("A1"),B1("B1");
Wizard A2("A2"),B2("B2");
Master A3("A3"),B3("B3");
pGroupA[0]=A1;
pGroupA[1]=A2;
pGroupA[2]=A3;
pGroupB[0]=B1;
pGroupB[1]=B2;
pGroupB[2]=B3;
}
class WarSystem
{
public:
WarSystem();
~WarSystem();//增加析构
void PrintSoldiers();
void startWar();
void startRound();
public:
Soldier *pGroupA[3];
Soldier *pGroupB[3];
private:
WarSystem( WarSystem const & );//禁止拷贝构造
WarSystem & operator = ( WarSystem const & );//禁止拷贝赋值
};
WarSystem::WarSystem()
{
std::fill(&pGroupA[0], &pGroupA[3], 0 );
std::fill(&pGroupB[0], &pGroupB[3], 0 );
Soldier A1("A1"),B1("B1");
Wizard A2("A2"),B2("B2");
Master A3("A3"),B3("B3");
pGroupA[0]=new Soldier ("A1");
pGroupA[1]=new Wizard ("A2") ;
pGroupA[2]=new Master("A3") ;
pGroupB[0]=new Soldier ("B1");;
pGroupB[1]=new Wizard ("B2") ;;
pGroupB[2]=new Master("B3") ;
}
WarSystem::~WarSystem()
{
for( int i = 0; i < 3; ++ i)
{
if(pGroupA[i])
{
delete pGroupA[i];
}
}
for( int i = 0; i < 3; ++ i )
{
if(pGroupB[i])
{
delete pGroupB[i];
}
}
}