나는 T와 같은 문자열을 입력 할 수있는 자리 표시 자 클래스로 템플릿 클래스를 만들려고합니다. 아래는 동일한 코드를 작성한 것입니다.C++ 다른 클래스의 자리 표시 자로 템플릿 클래스 만들기
#include <iostream>
#include <string>
#include <map>
using namespace std;
//A class which act as placeholder to hold
//unknown object. Something similar as Object
//in Java
template <typename T>
class Genric
{
public:
map<string, T> addP; //This will be placeholder for time
// being.
};
class A
{
public:
Genric t1; //Have object of Genric class so that we can
// access the member variable in future.
void foo()
{
cout<<"Calling foo"<<endl;
}
};
int main()
{
A a1;
a1.foo();
}
하지만 컴파일 할 때 오류가 발생했습니다.
$ g++ tempClass.cxx
tempClass.cxx:21:9: error: invalid use of template-name 'Genric' without an argument list
위의 Genric 클래스의 목적은 미래에 채울 수있는 멤버 변수 중 하나에 대한 자리 표시 자 클래스 역할을하는 것입니다. 그런 Genric 클래스를 작성할 수있는 방법이 있습니다.
map<string, boost::any> anywayzz;
당신은 그것에서 어떤 객체를 저장할 수 있습니다
이 작업을 수행 할 수있는 것처럼 보이지만 이러한 자리 표시 자 클래스를 갖는 최종 목표는 s11n :: deserialize를 사용하여 객체를 비 직렬화하는 것입니다.이 객체는 런타임시 기본 유형 비 직렬화를 수행 할 수 있지만 알 수없는 것이 있으면 어떻게 처리해야하는지 위의 접근법. – Abhinav
@Abhinav : 편집을 참조하십시오. –