자유 시간에 작은 게임을하고 있는데, 코딩하는 동안 문제가 생겼습니다.이것은 일종의 포인터 오류입니까?
나는 문제를 분리하고 여기에 가능한 한 작게했습니다
#include <iostream>
#include <memory>
#include <vector>
#include <Windows.h>
class Base
{
public:
std::string p;
virtual ~Base()
{
}
virtual std::vector<std::shared_ptr<std::string>> getText() = 0;
};
class Derived1 : public Base
{
public:
Derived1()
{
p = "from1";
}
std::vector<std::shared_ptr<std::string>> getText()
{
return std::vector<std::shared_ptr<std::string>>{std::shared_ptr<std::string>(&p)};
}
};
class Derived2 : public Base
{
public:
Derived2()
{
p = "from2";
}
std::vector<std::shared_ptr<std::string>> getText()
{
return std::vector<std::shared_ptr<std::string>>{std::shared_ptr<std::string>(&p)};
}
};
int main()
{
std::unique_ptr<Base> state;
std::vector<std::shared_ptr<std::string>> displayText;
state.reset(new Derived1());
displayText = state->getText();
while(1)
{
for(auto i = displayText.begin(); i != displayText.end(); ++i)
std::cout << **i;
if (GetKeyState('2') < 0)
{
state.reset(new Derived2());
displayText.clear();
displayText = state->getText();
}
else if (GetKeyState('1') < 0)
{
state.reset(new Derived1());
displayText.clear();
displayText = state->getText();
}
}
return 0;
}
"1"을 눌러 "2"앞뒤가 가끔 작동하는 것 같다, 나는 I을 종료 할 때 -1073741510 또는 이와 유사한 값을 반환 값으로 가져옵니다. 내 게임 코드에서 그것은 또한 많이 충돌하는 것 같다.
난 아직도 이유를 이해할 수 없다
다형성 클래스와 함께 일반적으로 일부 unique_ptr에 대한 연구 및 포인터를 수행 한 후 (출구 밖으로까지 나는 명확히하지 않는, 죄송합니다 명령 창에서 X 버튼을 사용하여 의미) 그것이하는 것처럼 행동한다.
여기에 무슨 일이 발생합니까?
"종료"를 정의하십시오. 표시된 코드는 프로그램을 완전히 종료 할 수있는 방법을 제공하지 않습니다. 그것은 무한 루프에서 실행됩니다. –
@SamVarshavchik이 (가) 고정되어 있습니다. 명확하지 않은 점에 대해 사과드립니다. –