Templates that only accept certain types and class having a template as friend
I have two questions:
First, is it possible to make a template that only takes classes that inherited from a certain base class as it's template parameter? Particularly, I want to make a sort of smart pointer class template (a pointer that detects whether the thing it points to was deleted by something else) that can point to an object of class GameObject or a class that inherits from GameObject. The reason I don't want it to point to anything else is because some of the methods required to make the smart pointer tick depend on a member function GameObject. The reason I want to use a template is because I do not want to downcast the inherited class, then upcast it later (which would require dynamic casting, checking whether it's of the right class, etc) so I specifically want a class that keeps a pointer of type pointer-to-derived-object. If this is possible, how do I do it?
Second, is it possible to have all classes spawned from a template as friend? Say a member function of the template needs to access a protected member of another class; how would I go about this?
Any suggestions to getting this to work in an easier way not involving templates of pointer casts would be very welcome as well!
Example header code I've already written:
Somewhere in a cpp file I'd call
unsigned int GetObjectNum();
virtual void Act() = 0;
// irrelevant stuff omitted
class FooBarrel : public GameObject
virtual void Act();
// stuff omitted
// stuff omitted
friend ObjectPointer; // Question 2: does this work?
bool ObjectIsValid(unsigned int);
extern ObjectList *grand_list;
// Question 1: how to limit T to classes derived from GameObject, like Foobarrel?
template <class T>
// This requires ObjectPointer<T> to be a friend of ObjectList
if (grand_list->ObjectIsvalid(objectnum) )
ObjectPointer operator=(T *in)
pointed_to = in;
objectnum = in->GetObjectNum(); // this requires T to be derived from GameObject
unsigned int objectnum;
I've searched several C++ tutorials and the archives of these forums for answers but couldn't find anything. :(
Thanks in advance!