Type conversion operators



 DEVELOP > c-Plus-Plus > Type conversion operators

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Eric"
Date: 22 Nov 2003 04:33:42 AM
Object: Type conversion operators
Say you have the following:
template <typename _T>
struct A {
typedef _T T;
A (T t);
operator T () const;
};
How do you define the T conversion operator?
TIA.
.

User: "Rob Williscroft"

Title: Re: Type conversion operators 22 Nov 2003 07:22:05 AM
Eric wrote in news:2e5db4bf.0311220233.7113f74a@posting.google.com:

Say you have the following:

template <typename _T>

Note that identifiers that begin with an underscore followed by one
of A through Z are reserved for the implementation, for eg your
std library implementation my define _T as a macro, don't use such
identifiers,

struct A {
typedef _T T;
A (T t);
operator T () const;
};

How do you define the T conversion operator?

template <typename Type >
struct A
{
typedef Type T;
A (T t) {}
operator T () const;
};
template < typename Type >
A< Type >::operator typename A< Type >::T () const
{
return T();
}
You could also in this case of writen this,
template < typename Type >
A< Type >::operator Type () const
{
return T(); // or Type();
}
Or (if inline is Ok) inside the class/struct,
template <typename Type >
struct A
{
typedef Type T;
A (T t) {}
operator T () const
{
return T();
}
};
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER