어떻게 해결할 수 있습니까? 나는 적절한 방법을 실행하고 싶다. 이 문제를 해결할 방법이 있습니까? 하나의 루프에서 메소드를 실행하려고합니다.C++ 상속, 어떻게 해결할 수 있습니까?
class Base
{
public:
void something() {}
};
class Child : public Base
{
public:
void something() {}
};
class SecondChild : public Base
{
public:
void something() {}
};
std::vector<Base*> vbase;
Child * tmp = new Child();
vbase.push_back((Base*) tmp);
SecondChild * tmp2 = new SecondChild();
vbase.push_back((Base*) tmp);
for (std::vector<Base*>::iterator it = vbase.begin(); it != vbase.end(); it++)
{
//here's problem, I want to execute proper method "something", but only I can do is execute Base::something;
(*it)->something();
}
기본 유형의 자식이 많을 때 형식을 변환하는 방법을 알지 못합니다.
가상 기능이 필요합니다. 그리고 스마트 포인터를 사용하십시오. – chris
Dupe? http://stackoverflow.com/questions/3328022/confused-with-c-inheritance?rq=1 – chris