| 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/
.
|
|
|
|

|
Related Articles |
|
|