Jak uzyskać instancję szablonu klasy z instrukcji if? (C ++)

Załóżmy, że mam szablon klasy, który ma członkapData, który jestAxB tablica dowolnego typuT.

template <class T> class X{ 
public:
    int A;
    int B;
    T** pData;
    X(int a,int b);
    ~X();        
    void print(); //function which prints pData to screen

};  
template<class T>X<T>::X(int a, int b){ //constructor
    A = a;
    B = b;
    pData = new T*[A];
    for(int i=0;i<A;i++)
        pData[i]= new T[B];
    //Fill pData with something of type T
}
int main(){
    //...
    std::cout<<"Give the primitive type of the array"<<std::endl;
    std::cin>>type;
    if(type=="int"){
        X<int> XArray(a,b);
    } else if(type=="char"){
        X<char> Xarray(a,b);
    } else {
        std::cout<<"Not a valid primitive type!";
    } // can be many more if statements.
    Xarray.print() //this doesn't work, as Xarray is out of scope.
}

Ponieważ instancja Xarray jest skonstruowana w instrukcji if, nie mogę jej używać nigdzie indziej. Próbowałem utworzyć wskaźnik przed instrukcjami if, ale ponieważ typ wskaźnika jest w tym momencie nieznany, nie udało mi się.

Jaki byłby właściwy sposób radzenia sobie z tego rodzaju problemem?

questionAnswers(4)

yourAnswerToTheQuestion