在Varian和基本数据类型之间怎样进行数据类型转换

Elfen 2003-12-16 12:06:30
在Varian和基本数据类型之间怎样进行数据类型转换
...全文
83 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sboom 2003-12-16
  • 打赏
  • 举报
回复
我知道 可以转成 _bstr_t
Elfen 2003-12-16
  • 打赏
  • 举报
回复
一模一样呀。。。。
checkyvc6 2003-12-16
  • 打赏
  • 举报
回复
我给你点详细的例子,看下面
先看懂_variant_t与_bstr_t这两个类的构造函数和 operator=
里面有重载了很多情况,
其他类型向_variant_t 赋值:
_variant_t( ) throw( );
_variant_t( const VARIANT& varSrc ) throw( _com_error );
_variant_t( const VARIANT* pVarSrc ) throw( _com_error );
_variant_t( const _variant_t& var_t_Src ) throw( _com_error );
_variant_t( VARIANT& varSrc, bool fCopy ) throw( _com_error );
_variant_t( short sSrc, VARTYPE vtSrc = VT_I2 ) throw( _com_error );
_variant_t( long lSrc, VARTYPE vtSrc = VT_I4 ) throw( _com_error );
_variant_t( float fltSrc ) throw( );
_variant_t( double dblSrc, VARTYPE vtSrc = VT_R8 ) throw( _com_error );
_variant_t( const CY& cySrc ) throw( );
_variant_t( const _bstr_t& bstrSrc ) throw( _com_error );
_variant_t( const wchar_t *wstrSrc ) throw( _com_error );
_variant_t( const char* strSrc ) throw( _com_error );
_variant_t( bool bSrc ) throw( );
_variant_t( IUnknown* pIUknownSrc, bool fAddRef = true ) throw( );
_variant_t( IDispatch* pDispSrc, bool fAddRef = true ) throw( );
_variant_t( const DECIMAL& decSrc ) throw( );
_variant_t( BYTE bSrc ) throw( );

operator=的重载形式:
_variant_t& operator=( const VARIANT& varSrc ) throw( _com_error );
_variant_t& operator=( const VARIANT* pVarSrc ) throw( _com_error );
_variant_t& operator=( const _variant_t& var_t_Src ) throw( _com_error );
_variant_t& operator=( short sSrc ) throw( _com_error );
_variant_t& operator=( long lSrc ) throw( _com_error );
_variant_t& operator=( float fltSrc ) throw( _com_error );
_variant_t& operator=( double dblSrc ) throw( _com_error );
_variant_t& operator=( const CY& cySrc ) throw( _com_error );
_variant_t& operator=( const _bstr_t& bstrSrc ) throw( _com_error );
_variant_t& operator=( const wchar_t* wstrSrc ) throw( _com_error );
_variant_t& operator=( const char* strSrc ) throw( _com_error );
_variant_t& operator=( IDispatch* pDispSrc ) throw( _com_error );
_variant_t& operator=( bool bSrc ) throw( _com_error );
_variant_t& operator=( IUnknown* pSrc ) throw( _com_error );
_variant_t& operator=( const DECIMAL& decSrc ) throw( _com_error );
_variant_t& operator=( BYTE bSrc ) throw( _com_error );

有了以上两个函数,举个例子:
double f=1.0
_variant_t v;
v=f; //是合法的看看operator=的重载形式就知道了
CString str="ddd"
_variant_t v;
v=str.AllocSysString() 或者v=(_bstr_t)(char*)str;
即可

_variant_t转换成别的形式
你首先必须确定你要转化成什么样的形式
double f;
_variant_t v
f=v.dblVal 即可或者f=(double)v;也可以

附:_variant_t的操作符
operator short( ) const throw( _com_error );
operator long( ) const throw( _com_error);
operator float( ) const throw( _com_error );
operator double( ) const throw( _com_error );
operator CY( ) const throw( _com_error );
operator bool( ) const throw( _com_error );
operator DECIMAL( ) const throw( _com_error );
operator BYTE( ) const throw( _com_error );
operator _bstr_t( ) const throw( _com_error );
operator IDispatch*( ) const throw( _com_error );
operator IUnknown*( ) const throw( _com_error );


vcforever 2003-12-16
  • 打赏
  • 举报
回复
我给你点详细的例子,看下面
先看懂_variant_t与_bstr_t这两个类的构造函数和 operator=
里面有重载了很多情况,
其他类型向_variant_t 赋值:
_variant_t( ) throw( );
_variant_t( const VARIANT& varSrc ) throw( _com_error );
_variant_t( const VARIANT* pVarSrc ) throw( _com_error );
_variant_t( const _variant_t& var_t_Src ) throw( _com_error );
_variant_t( VARIANT& varSrc, bool fCopy ) throw( _com_error );
_variant_t( short sSrc, VARTYPE vtSrc = VT_I2 ) throw( _com_error );
_variant_t( long lSrc, VARTYPE vtSrc = VT_I4 ) throw( _com_error );
_variant_t( float fltSrc ) throw( );
_variant_t( double dblSrc, VARTYPE vtSrc = VT_R8 ) throw( _com_error );
_variant_t( const CY& cySrc ) throw( );
_variant_t( const _bstr_t& bstrSrc ) throw( _com_error );
_variant_t( const wchar_t *wstrSrc ) throw( _com_error );
_variant_t( const char* strSrc ) throw( _com_error );
_variant_t( bool bSrc ) throw( );
_variant_t( IUnknown* pIUknownSrc, bool fAddRef = true ) throw( );
_variant_t( IDispatch* pDispSrc, bool fAddRef = true ) throw( );
_variant_t( const DECIMAL& decSrc ) throw( );
_variant_t( BYTE bSrc ) throw( );

operator=的重载形式:
_variant_t& operator=( const VARIANT& varSrc ) throw( _com_error );
_variant_t& operator=( const VARIANT* pVarSrc ) throw( _com_error );
_variant_t& operator=( const _variant_t& var_t_Src ) throw( _com_error );
_variant_t& operator=( short sSrc ) throw( _com_error );
_variant_t& operator=( long lSrc ) throw( _com_error );
_variant_t& operator=( float fltSrc ) throw( _com_error );
_variant_t& operator=( double dblSrc ) throw( _com_error );
_variant_t& operator=( const CY& cySrc ) throw( _com_error );
_variant_t& operator=( const _bstr_t& bstrSrc ) throw( _com_error );
_variant_t& operator=( const wchar_t* wstrSrc ) throw( _com_error );
_variant_t& operator=( const char* strSrc ) throw( _com_error );
_variant_t& operator=( IDispatch* pDispSrc ) throw( _com_error );
_variant_t& operator=( bool bSrc ) throw( _com_error );
_variant_t& operator=( IUnknown* pSrc ) throw( _com_error );
_variant_t& operator=( const DECIMAL& decSrc ) throw( _com_error );
_variant_t& operator=( BYTE bSrc ) throw( _com_error );

有了以上两个函数,举个例子:
double f=1.0
_variant_t v;
v=f; //是合法的看看operator=的重载形式就知道了
CString str="ddd"
_variant_t v;
v=str.AllocSysString() 或者v=(_bstr_t)(char*)str;
即可

_variant_t转换成别的形式
你首先必须确定你要转化成什么样的形式
double f;
_variant_t v
f=v.dblVal 即可或者f=(double)v;也可以

附:_variant_t的操作符
operator short( ) const throw( _com_error );
operator long( ) const throw( _com_error);
operator float( ) const throw( _com_error );
operator double( ) const throw( _com_error );
operator CY( ) const throw( _com_error );
operator bool( ) const throw( _com_error );
operator DECIMAL( ) const throw( _com_error );
operator BYTE( ) const throw( _com_error );
operator _bstr_t( ) const throw( _com_error );
operator IDispatch*( ) const throw( _com_error );
operator IUnknown*( ) const throw( _com_error );

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧