여기 상황이 있습니다. 가상 기본 클래스 (예 : ShapeJuggler
)에 가상 기본 클래스 객체 (예 : Shape
)에 대한 공유 포인터를 인수로 사용하는 메소드가 포함되어 있다고 가정 해 보겠습니다. 의 이해 다음 의사 코드에 뛰어 보자C++ 상속 및 공유 포인터
class Shape {
}
class ShapeJuggler {
virtual void juggle(shared_ptr<Shape>) = 0;
}
// Now deriving a class from it
class Square : public Shape {
}
class SquareJuggler : public ShapeJuggler {
public:
void juggle(shared_ptr<Shape>) {
// Want to do something specific with a 'Square'
// Or transform the 'shared_ptr<Shape>' into a 'shared_ptr<Square>'
}
}
// Calling the juggle method
void main(void) {
shared_ptr<Square> square_ptr = (shared_ptr<Square>) new Square();
SquareJuggler squareJuggler;
squareJuggler.juggle(square_ptr); // how to access 'Square'-specific members?
}
make_shared 또는 동적/static_cast는 일을하지 않는 것. 모두 가능합니까? 어떤 아이디어, 제안?
감사합니다.
'main()'은'void'가 아니라'int'를 반환해야합니다. –
"_we는 가상 기본 클래스가 있습니다."귀하의 예제 (또는 "의사 코드")에 ** 가상 ** 키워드를 잊어 버렸습니다. – curiousguy