에서 데이터를 검색하는 방법. 클라이언트 클래스는이 Controller 클래스를 통해 프로그램에서 사용할 구체적인 클래스 객체를 추가/제거/편집 할 수 있습니다.나는 개인 <code>std::list</code>와 싱글 컨트롤러 클래스에서 작업하는 <code>std::list<boost::shared_ptr<boost::any>></code></p> <p>에서 개최 된 데이터를 검색하려고 도움이 필요 향상을 포함하는 표준 : : 목록 :: shared_ptr의
boost::shared_ptr
을 사용하는 이유는 내가 만든 각 구체적인 클래스에 고유 한 objID를 할당했기 때문입니다. 인스턴스 obj가 컨트롤러에 추가되면 사용자는 나중에 obj를 검색하고 제거 할 수 있습니다. 각 구체적인 클래스에 대해 Add(....)
및 Remove(...)
오버로드 된 메서드가 정상적으로 작동합니다.
는 지금 getObject(int index)
& setObject(int index)
방법을 만들기 위해 노력하고 있지만 구체적인 클래스에 반환 된 포인터를 캐스팅하는 방법을 알아낼 수 없습니다.
알려 주시기 바랍니다.
내 현재 코드 :
//===============================================================
//Singleton.h controller class
private:
static Singleton *mgr;
typedef boost::shared_ptr<boost::any> Shapes_Ptr;
//private static list
static std::list<Shapes_Ptr> shapes;
public:
const Shapes_Ptr getObject(int index) const; //Return Shape
Shapes_Ptr EditObject(const int index); //Edit Shape
Info(); //Prints contents of instance to console screen
//===============================================================
//Singleton.cpp
//Return Shape
const Shapes_Ptr getObject(int index) const
{
int cc = 0;
if ((int)shapes.size() > ZERO && index < (int)shapes.size())
{
list<Shapes_Ptr>::const_iterator i;
for (i = shapes.begin(); i != shapes.end(); ++i)
{
if (cc == index)
{
return (*i);
break;
}
else { ++cc; }
}//for-loop
}
}
//Edit Shape
Shapes_Ptr EditObject(const int index)
{
//same code as getObject()...
}
//===============================================================
//main.cpp
Singleton *contrl= Singleton::Instance();
int main()
{
for (int i=0; i< 2; ++i)
{
contrl->CreateObject(Box2D());
}
for (int i = contrl->Begin(); i< contrl->End(); ++i)
{
if (boost::any_cast<boost::any> (contrl->getObject(i)).type() == typeid(Physics::Box2D))
{
//Code compiles but crashes on launch....
any_cast<Box2D> (contrl->getObject(i)).Info(); // <== ERROR CODE
}
//More if checks for other Concrete classes....
}
}
달성하고자하는 것을 설명해 주시겠습니까? 디버거에서 기존 코드를 실행 해 보셨습니까? –
@ 존 - 예, 프로젝트를 디버그 모드로 실행했습니다. 예외 오류'bad_cast'가 발생했습니다. private'std :: list'에 대한 getter/setter를 만들려고합니다. 그래서 데이터를 반복하고 인쇄 할 수있을뿐 아니라 각 데이터 멤버의 setter를 호출 할 수 있습니다. 목록의 각 요소 목록의 요소를 깨끗하게 또는 더 잘 보여줄 수있는 방법이 있다면 조언하십시오. – badboy11
그래서이 코드는 무엇에 좋은가? 왜 그 유형에 포함되어 있는지 모르는 싱글 톤을 원하지만 어쨌든 포함하고 싶습니까? –