학교 과제에 문제가 있습니다. 스포츠 경기에서 경쟁자를 추적하는 시스템을 만들어야합니다. 나는 기본 클래스 경쟁자와 그로부터 파생 된 두 클래스, 즉 Professional
과 Exerciser
을 가지고있다.
그리고 나서 Contender **contenders
을 포함하는 레지스터 클래스가 있습니다. 그리고이 클래스에 대한 복사 생성자를 만들어야하지만 어떻게 해야할지 모르겠습니다.상속을 사용하는 복사 생성자
은 내가 contenders
포인터의 배열에 대한 포인터 것을 이해이
Register::Register(const Register& original)
{
this->kap = original.kap;
this->currentAmount = original.currentAmount;
for (int i = 0; i < this->currentAmount; i++)
{
if (Professional* pro = dynamic_cast<Professional*>(this->contenders[i]))
{
this->contenders[i] = new Professional(*original.contenders[i]);
}
if (Exerciser* pro = dynamic_cast<Exerciser*>(this->contenders[i]))
{
this->contenders[i] = new Exerciser(*original.contenders[i]);
}
}
this->initiate(this->currentAmount);
}
그래서 당신이 발생하는 문제가 무엇 이 코드? [virtual constructor idiom] (http://en.wikibooks.org/wiki/More_C++_Idioms/Virtual_Constructor) (특히 가상 복사본)을 살펴볼 수 있습니다. –
그렇다면'Register' 클래스는 모든 가능한 파생 클래스를 알아야합니다. 아마도 당신은 모든 파생 클래스가'Base * clone() const {new new Derived (* this);}로 구현하는'virtual base * clone() const; 멤버 함수를 기본 클래스에 부여 할 수 있습니다. }' – BoBTFish
http://stackoverflow.com/questions/259853/whats-the-best-signature-for-clone-in-c – abcthomas