So, I have a generic operator. In this specific case its generic template T
is supposed to be a pointer to an abstract base class. The point is, when I create T *t
and try to return *t
I get an error saying T
can't be instantiated because it's an abstract class. Even though *t
will always be an object of derived classes which aren't abstract. It doesn't accept it, because T is after all abstract type. Basically, what I want to achieve is to get a reference to the *t
returned from the operator
to main()
and then assign to it objects of derived classes in main()
.
I understand that there are questions about slicing and polymorphism. But I am trying to understand how can I achieve it in different ways. It's a completely different question which deals with the following specific code.
Code is something like this:
Template <class T>
class myArr{
T & operator [] (int index){
T*t;
Arr[0] = *t;
Return arr[0[;
}}
Main(){
Base * b=new derived;
myArr<Base>[17]=*b;
// Point is, I can make it of type pointers and maybe it can
//work but I prefer assigning *b to myArr because I want
to //assign objects themselves
}
My question is, how can I return the object in itself from the operator
? I want to return something to which I can assign a derived object . I am trying to do it with pointer of type abstract base, so as to achieve polymorphism so it can contain any object of derived classes.