I have created a template storage class which contains the following function:
template<class X>
void Queue<X>::insert(X& q)
{ X* p = new X(q);
.
.
}
By invoking the copy constructor in the class plugged in for X the storage class stores an exact copy of the object passed as an argument. (This is for programming reasons.) It works fine if what is plugged in for X is an object. But if a base class pointer is plugged in there is an error; memory is allocated but no type of constructor is invoked so that if I try to access the stored object the program crashes. I have defined constructors in the base and derived classes that take a pointer as an argument but those functios are never invoked. How can I force some type of constructor to be invoked when the template argument happens to be a base class pointer.
I would be grateful for all help.
template<class X>
void Queue<X>::insert(X& q)
{ X* p = new X(q);
.
.
}
By invoking the copy constructor in the class plugged in for X the storage class stores an exact copy of the object passed as an argument. (This is for programming reasons.) It works fine if what is plugged in for X is an object. But if a base class pointer is plugged in there is an error; memory is allocated but no type of constructor is invoked so that if I try to access the stored object the program crashes. I have defined constructors in the base and derived classes that take a pointer as an argument but those functios are never invoked. How can I force some type of constructor to be invoked when the template argument happens to be a base class pointer.
I would be grateful for all help.