영어 설명 C++에서 추상 기본 클래스입니다지도에서 요소의 회원 : 나는 std::map<string, Parent *>
곳 Parent
에서 "bar"
의 키 요소에서 foo
라는 이름의 멤버에 액세스 할은 추상적이다 기본 클래스.액세스 값
코드 :
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Parent {
public:
virtual ~Parent() {}
};
class Child: public Parent {
public:
Child(): var(2) {}
int var;
};
int main() {
map<string, Parent *> children;
children["bar"] = new Child;
cout << children["bar"]->var << endl; // Erroneous line
cout << children.find("bar")->second->var << endl; // Different method, still gives the same error
return 0;
}
오류 :‘class Parent’ has no member named ‘var’
.
나는 또한 boost::ptr_map
을 사용해 보았습니다.
int main() {
boost::ptr_map<string, Parent> children;
string key = "bar";
children.insert(key, new Child);
cout << children.find(key)->second->var << endl; // Same error :(
return 0;
}
컨텍스트의 비트 : 내가 [엔티티 컴포넌트 시스템]을 구현하고있어 (http://en.wikipedia.org/wiki/Entity_component_system) 여기서'Parent'에서 실제 프로그램을 , 모든 구성 요소가 상속하는 클래스가됩니다. (올바른 용어입니까?) – mcjohnalds45
가상 액세스는 데이터 변수가 아닌 함수 용입니다. –