By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Type conversion operators

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Eric wrote in news:2e**************************@posting.google.c om:
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/
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.