Search code examples
c++ooppointerstemplate-classes

Template array of pointers c++?


Hello guys in my c++ program I have four classes (A,B,C,D)

  • A is the base class
  • B inherits from A
  • C inherits from A
  • D inherits from B

All of them are template classes template<class Type> and each of them has a print method that prints its private members and the private members of the class that it inherits from.

So B would print B private members and A private members, C would print C private members and A private members, D would print its private members and B,A private members.

In the main function I want to create an Array of pointers for class A that have 3 locations for an object of each class then I want to loop each object print method.

the problem is when I change the classes to a template classes I got an error message says "that my classes don't have constructors" ; however they do have them.

here is my code Please Help (Note I commented where the errors occurs for you):

#include <iostream>
#include <string>
using namespace std;

template <class Type>
class A
{
public:
virtual void print()
{
    cout<<"the base class (A) private (x) is : "<<x<<endl;
}

A(Type X = 0)
{
    x = X;
}
void setX(Type X)
{
    x = X;
}

Type getX() const
{
    return x;
}

private:
Type x;
};


template <class Type>
class B:public A
{
public:
B(Type X = 0,Type Y = 0)
{
    setX(X);
    y = Y;
}
void setY(Type Y)
{
    y = Y;
}

Type getY() const
{
    return y;
}

void print()
{
    A::print();

    cout<<"private (y) in class (B) is : "<<getY()<<endl;
}

private:
Type y;
};

template <class Type>
class C:public A
{
public:
C(Type X = 0,Type Z = 0)
{
    setX(X);
    z = Z;
}
void setZ(Type Z)
{
    z = Z;
}

Type getZ() const
{
    return z;
}

void print()
{
    A::print();

    cout<<"private (z) in class (C) is : "<<getZ()<<endl<<endl;
}

private:
Type z;
};


template <class Type>
class D:public B
{
public:
D(Type X = 0,Type Y = 0,Type W = 0)
{
    setX(X);
    setY(Y);
    w = W;
}
void setW(Type W)
{
    w = W;
}

Type getW() const
{
    return w;
}

void print()
{
    B::print();

    cout<<"private (w) in class (D) is : "<<getW()<<endl;
}

private:
Type w;
};


void main()
{
A<int>* arrayOfPointers[3];

arrayOfPointers[0] = new B(1,100);//error here
arrayOfPointers[1] = new C(2,200);//error here
arrayOfPointers[2] = new D(3,300,3000);//error here

for(int i = 0 ; i<3;i++)
{
    cout<<typeid(*arrayOfPointers[i]).name()<<" Print method : \n"<<endl;
    arrayOfPointers[i]->print();
    cout<<"**********************\n"<<endl;
}

}

Solution

  • You forgot two things:

    1) Your inheritance needs to specify template arguments for the class they are inheriting from. For example:

    template <class Type>
    class B : public A<Type>
    {
        ...
    }
    

    2) When you are instantiating your classes, you need to provide template arguments, too:

    arrayOfPointers[0] = new B<int>(1, 100);
    arrayOfPointers[1] = new C<int>(2, 200);
    arrayOfPointers[2] = new D<int>(3, 300, 3000);
    

    However, you can also provide template functions to instantiate these classes from the provided arguments, like those make_(...) methods from the std library:

    template <class Type>
    B<Type>* create_B(const Type& t1, const Type& t2)
    {
        return new B<Type>(t1, t2);
    }
    

    and use it like this:

     arrayOfPointers[0] = create_B(1, 100);
    

    However, be aware that those methods are creating raw pointers to allocated heap memory, so you are responsible for deleting it (you might use shared_ptrs or whatever to overcome that or just return an object etc but this is not actually part of your question/my answer).