나는 C++ (C++ 11 이전 버전이고 지금은 업그레이드 할 수 없다)와 관련된 질문 (템플릿 프로그래밍 (및 "특성")과 관련하여)이 있습니다.for 루프에서 변경할 템플릿 매개 변수는 무엇입니까?
내 목표 :
내가 (새로운 기능 및 회원들과 기본 클래스에서 이미 파생 클래스) 다른 (그러나 매우 유사한) 클래스가 있습니다.
나는 이러한 클래스의 상속 해, 데이터베이스의 모든 회원들과 특정 클래스에 관련된 정보에서 충전 "열기"다형성 함수를 사용하는 템플릿 클래스를 프로그램.이 인스턴스화 된 클래스 (모든 멤버 포함)를 다른 함수의 입력으로 사용하기 때문에이 전략을 생각했습니다. 나는 그것을 할 수 있었다 (나는 생각한다. 그러나 나의 템플릿 클래스는 템플릿 매개 변수의 클래스로부터 상속받을 수있다.) 스위치/케이스 아키텍처와 함께, 그러나 나는 이미 그것을 나중에 많이 사용하기 때문에 여기서 그것을 피하고 싶었다.
예를 들어 나는 Derived.hpp 파일에 정의 된 Derived1 및 Derived2 클래스를 사용하여 Root 부모 클래스의 함수를 덮어 씁니다. 나는 한 번 사용했던 템플릿 함수 MyClass를 가지고있다. MyClass < DerivedType1> currentClass() 또는 MyClass < DerivedType2> currentClass()는 내가 원하는 (아래 코드) 것을 수행한다.
질문 :
이 나에게 루프를 만들 수있는 가능성을 제공 무언가를 쓸 수 있습니까?
뭔가 지금 나를 위해
For (auto i=1; i<N; ++i)
{
MyClass<DerivedType[i]> currentClass();
--- other things to do with my currentClass ---
}
내 DerivedType (들) "유형"(아래 traits.hpp 조각의 구조체 참조)이며, 나는 심지어 내가 넣을 수 있는지 여부를 알 수없는 것 (벡터처럼) 컨테이너 안에 ... 어쩌면 C++ 11에서 모든 DerivedTypes에 대해 enum 클래스를 정의 할 수 있었습니까 (사실입니까?), 여기에 C++ 03이 있습니까?
는 완전히 분실에, 나는
이 어떤 제안에 대해 사전에 대단히 감사 ... 인정합니다.
(일) 주요
여기
int main(int, char* [])
{
GlobalClass GlobalItem(); //global class encapsulating all the info of each item from the database
//connection to the database
//DerivedType1 case
MyClass<DerivedType1> CurrentClass();
GlobalItem.AddCurrentClass();
//with a for loop or the like I can use the fact that at each loop the class is declared only inside the { … } and then each time the destructor
//is automatically called
CurrentClass.clear();
CurrentClass = MyClass<DerivedType2>();
GlobalItem.AddCurrentClass();
return 0;
}
템플릿 클래스 MyClass.hpp이다 ("MyClass.hpp"를 포함) :
는 ("traits.hpp"를 포함)
template <class Traits>
class MyClass : public Traits::type
{
private:
typedef typename Traits::type BaseType;
protected:
std::string currentType_;
public:
//constructor
MyClass() : BaseType() { this->open() }
//destructor
virtual ~MyClass();
};
범위 :: operato에 의해 실행되는 traits.hpp 파일 R
("Derived.hpp"를 포함)
struct DerivedType1 {
typedef Derived1 type;
};
struct DerivedType2 {
typedef Derived2 type;
};
[. 아니 (http://www.reactiongifs.com/wp -content/gallery/no/john-cleese-no.gif) – jrok