Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

farmersckn

Typedefs as member of a template...

This topic is 6344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say you have a template, like this:
  
template <class T>
class Temp
{
  // typedefs

  typedef T type;
  typedef T * type_pointer;
  typedef Temp & temp_reference;

  // constructor

  Temp (temp_reference trTemp);

  // etc...

};
// Now, I want to define methods outside the class def.

// When I do this, however, it won''t work, because I get an

// error that says that you can''t use a qualifier to access

// typedefs (i.e.  ClassName::TypedefName is illegal).

// Another thing, I was hoping that I could use the names of 

// the typedefs instead of T and T * etc...

template <class T>
Temp::Temp (Temp::temp_reference trTemp)
{

}

// I think that should show what I want...

  
Does anyone have an Idea to help me? I''m open to just about anything, as long as it works, without destroying clarity.

Share this post


Link to post
Share on other sites
Advertisement
Here is what I did:

  
//template.h

#ifndef __TEMPLATE_H_
#define __TEMPLATE_H_

template <class T>
class Temp
{
typedef T type;
typedef T* type_ptr;
typedef T& type_ref;

public:
Temp();
~Temp();

public:
void ChangeValue( type_ref value );
type Asign( type value );

};

#include "template.cpp"
#endif

//template.cpp

#ifndef __TEMPLATE_CPP_
#define __TEMPLATE_CPP_

#include "template.h"

template<class T>
Temp<T>::Temp()
{
}

template<class T>
Temp<T>::~Temp()
{
}

template<class T>
void Temp<T>::ChangeValue( type_ref value )
{
value *= value;
}

template<class T>
T Temp<T>::Asign( T value )
{
return value;
}

#endif

//dos_main.cpp

#include <iostream.h>
#include "template.h"

void main()
{
Temp<int> val;

int value = 6;

val.ChangeValue( value );
int r = val.Asign( value );

cout << r << endl;
}


It compiled and ran as you would expect. I don''t think that you actually need to put the Temp:: in front of the types in the parameter list. I didn''t in the example above and it works (using VC++ 6). Hope this helps.

Useless Code

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!