Link to home
Start Free TrialLog in
Avatar of Laminamia063099
Laminamia063099

asked on

Generic Class Definition

Please give me a simple example (or a very good hyperlink of the same) of how to create a generic class in C++.  By generic, I mean one that the user can instantiate with different data types.  To receive full credit, I would like to see in this example how the user must supply a function that should be overloaded (like the "=" operator) and how items of this unknown data type are used in the generic definition.  I have coded this in Ada95, but have yet to learn how to do it with C++.  

If my question is hard to understand, please leave a comment for me.  

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KangaRoo
KangaRoo

In this case, the destructor must be overload (called by delete pointer). This could be made more explicit by replacing
  delete pointer;
with
  pointer->DestroyYourself()
In which case that function has to be provided by the user class
Avatar of Laminamia063099

ASKER

(Wow, I thought I knew what I was doing, but sometimes I just feel like I'm in over my head.)

So, what you mean, KangaRoo, is that I would inherit from this class to be able to use it's functionality with a specific data type?
Sorry for my ignorance, but what is T?  Where does it come from?
T is some yet undefined type (or class). It is to be defined by the user, who can then pass it as parameter of the template:

class MyClass {};
sole_owner<MyClass> some_identifier;
Instantiates the template class (generates code for this particular type), and of course an object (named
 some_identifier) of that particular class instance.

You don't need to inherit from this class, although it is possible (not recommended though, why is left as exercise for the reader :)
It is my auto_ptr implementation and used as:

void f()
{
  ...
  sole_owner<SomeResource> resource = new SomeResource;
  ...
  resource->SomeMember();
  ...
} // sole_owner automatically deletes the containe object.
Thank you for your help.  The points are yours, though I have one more question... What is a template? (as compared to a class).
How does the class template recognize that the type inside < > replaces T in the template  (or am I way off)

Thanks for your help again.  

The more I learn, the more I realize how little I know.
A template is just that. Basically it defines a code pattern for classes and functions. Difficult to explain actually. Compared to a common class, it describes the classes that are instantiated from it.

You can also have templates for functions:

template<class T>
T max(const T& t1, const T& t2)
{ return t1 < t2 ? t2 : t1; }
>> How does the class template recognize that the type inside < > replaces T in the template
Look at it as a typedef. You specify the name of the type in the declaration of the template. like

template<class T> void f(T t)
{
   T temporary;
   ...
}

Within the template, T is considered as a declared class or type. As soon as it is used, eg:
  int i = 5;
  f(i);
it is instantiated for that type:
void f( int t )
{
  int temporary;
  ...
}

You don't have to use T, thats just a convention (short for Type). You can also use more meaningfull names:
template< class Key, class Value > class map;
Thanks a lot!  It makes perfect sense, and it's not so hard to understand (though I'm sure I'm oblivious to the finer points for a long time to come).  So, you don't have to instantiate it so much, but you can just use it as is (as long as all the operators are defined for the type you use)?  Where it says, template<class T>void f (T t), does it say "class" for anything you plan to instantiate it with, no matter it be a class or a regular data type?  In other words, is class the buzz-word always used in the template definition?  

Thanks again, I owe you one!
Would this be correct:

template<class T>class MyTemplateClass {
  T * tPtr; //....
}

To instantiate:
class X { int i };
X x;
MyTemplateClass mine(x);  //T is now replaced by type X.
Use
  MyTemplateClass<int> mine;
to instantiate the template. Note that this will also create an object of class MyTemplateClass<int> (named 'mine'). Iow, you have to specify the type arguments when instantiating template class.

Off course you could pass parameters to a constructor:
template<class T>class MyTemplateClass {
  T * tPtr;
  MyTemplateClass(T*) ;
  //....
} ;


class X { int i };
X* pX = new X;
MyTemplateClass<X> mine(x);
 
 
 
   
 

>> Where it says, template<class T>void f (T t), does it
>> say "class" for anything you plan to instantiate it
>> with, no matter it be a class or a regular data type?  

Yes, the class keyword within the '<>' can be replaced with the typename keyword, which emphasizes your point
  template<typename T> void f(T t);
which is the same as
  template<class T> void f (T t);
Thanks again! I'll try to read up more so I don't have to ask so many questions, but I may be back again!

Thanks for answering more than what you got points for, I owe you!
Your welcome, templates can be quite intimidating at first, but very rewarding to work with.
I know.  I wrote generic packages in Ada, which is the same thing.  It was about a year ago, and getting started with generic code is, as you say, intimidating!