Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

typedef a template class 1

Status
Not open for further replies.

djimm100

Programmer
May 19, 2003
8
0
0
US
I have a header file with a class declared like so:
template <class Item> class ItemJArray { ... };

I want to typedef this in a source file, but this does not work:
typedef ItemJArray JArray;

I want JArray to behave exactly like ItemJArray. Specifically, I want to be able to instantiate different objects of type JArray that operate on different data types.

How do I do this?
 
Well, that's because you can't typedef something that's not a type.

What you want is a template typedef:

Code:
  template< typename T >
class ItemJArray<T> JArray<T>;

Unfortunately, that doesn't exist in C++... yet. There's about a 90% chance there'll be one in the next C++ Standard, but it's not there now (unless you have a compiler that happens to support it as an &quot;extra&quot;).


The workaround is kinda clunky, but it works:

Code:
  template< typename T >
struct JArray<T>
{
    typedef ItemJArray<T> Type;
};

Then just use JArray<T>::Type. Of course, if all you were trying to do is shave of that &quot;Item&quot; part, that kinda defeats the purpose. If you've got something more complex, though, then that's the current solution.
 
Whoops.

That shoulda been:

Code:
  template< typename T >
struct JArray
{
    typedef ItemJArray<T> Type;
};


Also, I think the template typedef would be:

Code:
  template< typename T >
typedef ItemJArray<T> JArray;

I guess it would help if it had the word &quot;typedef&quot; in it, huh? Oh, well. Can't fault me for being wrong on syntax that doesn't exist yet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top