分配变量和类的类型 [英] Assign type of variable and class

查看:91
本文介绍了分配变量和类的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

请帮助我,让我问一个愚蠢的问题。

我将变量x声明为DeriveType(DeriveType是一个类)。

我想将一个int y分配给x,例如

int y = x

但是当我编译时,编译器显示错误

错误:无法将'const DerivType'转换为'int'

请告诉我如何解决这个问题。

非常感谢。

你真诚的,



这是Class DerivType



Dear folk,
Please help me and let me ask a stupid question.
I declared variable x as DeriveType (DeriveType is a class).
I want to assign an int y to x, e.g
int y = x
But when I compile, the compiler shows the error
error: cannot convert 'const DerivType' to 'int'
Please tell me how to solve this problem.
Thank you very much.
Yours sincerely,

This is the Class DerivType

class DerivType;

typedef DerivType (*ddf_FctPtr)(const DerivType&);

class DerivType { private: interval f, df, ddf;

public: DerivType ( ); 
DerivType ( const interval&, const interval&, const interval& ); 
DerivType ( const DerivType& );

DerivType& operator= ( const DerivType& );

friend DerivType DerivConst ( const real& );
friend DerivType DerivConst ( const interval& );
friend DerivType DerivVar   ( const real& );
friend DerivType DerivVar   ( const interval& );

friend inline const interval fValue   ( const DerivType& );  
friend inline const interval dfValue  ( const DerivType& );  
friend inline const interval ddfValue ( const DerivType& );  

friend DerivType operator+ ( const DerivType& );
friend DerivType operator- ( const DerivType& );

.... 





我有一个喜欢这个的功能。





And I have a function likes this.

DerivType f ( const DerivType& x )
{
        DerivType result;
        DerivType xx;
        double sum;
        xx = x;
        //Convert x from DerivType to double
        void *pVoidx = &xx;                                     [1]
        double *pDoublex = static_cast<double*>(pVoidx);        [2]
        MLPutReal(lp, *pDoublex);

        MLGetReal(lp, &sum);

        //Convert from double to DerivType for the return value of function
        printf( "x = %f, result = %f\n", *pDoublex, sum);
        void *pSum=&sum;                                            [3]
        DerivType *pDerivTypeSum = static_cast<DerivType*>(pSum);   [4]
    return *pDerivTypeSum;
}





此功能正常运行。但是我的老师说这不是好的代码,因为即使代码在这种情况下工作,它仍然依赖于实现

和架构相关。 (他给我链接: http://www.informit.com/guides/content .aspx?g = cplusplus& seqNum = 195 [ ^ ])



你能帮助我改进这段代码,即[1],[2],[3] ,[4]。

我的想法是如何将DerivType转换为double / int,反之亦然。

感谢您的阅读和帮助。我非常赞赏你的提示。

你的真诚



非常感谢

你诚挚的,



This function work properly. But my teacher said that it is not the good code, because even the code works in this case, it is still implementation-dependent
and architecture-dependent. (He gives me the link: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=195[^])

Can you help me to improve this code, namely [1], [2], [3], [4].
My idea is how to convert from DerivType to double/int and conversely.
Thank for your reading and help. I am very appriciate about your hints.
Your sincerely

Thank you very much
Yours sincerely,

推荐答案

没有什么可解释的。分配需要右侧的赋值兼容对象。你的 DeriveType 不是整数类型。



继承,要赋值的变量和对象在赋值权运算符可以是不同类型的。当要分配的变量属于基本类型,并且要分配的对象具有更多派生类型时,就是这种情况。这是可能的,因为继承只添加成员。赋值后的变量将具有基类的编译时类型,但派生类的运行时类型。这很好,因为所有编译时成员将始终存在于实际的运行时对象中。情况恰恰相反:它会让您访问不存在的成员,结果不可预测。违反OOP原则,总是可以使用动态转换 向下转换类型,它会检查对象的实际运行时类型及其赋值兼容性。你以后可以学习它。



但整数或其他原始类型与继承无关。之间的分配规则与值集的 width 相关。比如,右边的整数值可能超出左边变量的范围。但这是完全不同的故事。您不能在基元类型和类/结构之间进行分配。毕竟,你背后的逻辑是什么,它可能意味着什么?



-SA
There is nothing to explain. Assignment requires assignment-compatible object on the right. Your DeriveType is not an integer type.

With inheritance, the variable to be assigned to, and the object to be on the right of assignment operators can be of different types. This is the case when variable to be assigned to is of the base type, and the object to be assigned is of more derived type. This is possible because inheritance only add members. The variable after assignment will have the compile-time type of base class, but runtime type of the derived class. This is fine, because all compile-time members will always present in the actual runtime object. The opposite is not the case: it would give you access to non-existing members, with unpredictable results. In violation of OOP principles, it's always possible to down-cast the type with the dynamic cast, which checks up actual runtime type of the object and its assignment compatibility. You will be able to learn it later.

But integer or other primitive types have nothing to do with inheritance. The assignment rules between then are related to the "width" of the value sets. Say, the integer value on right could be beyond the range of the variable on left. But this is totally different story. You cannot assign between primitive types and classes/structures. After all, what would be your logic behind that, what would it possibly mean?

—SA


这篇关于分配变量和类的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆