用户定义类型的static_cast

时间:2022-03-07 16:29:13

Is it possible and why one would want to do it?

这是可能的吗?为什么要这么做?

class Foo;
class Bar;

......

Foo foo;
Bar bar = static_cast<Bar>(foo);

Normally static_cast is used with numeric types and pointers, but can it work with user defined data types, a.k.a classes?

通常,static_cast用于数值类型和指针,但它是否可以用于用户定义的数据类型,a.k。类?

2 个解决方案

#1


10  

Bar bar = static_cast<Bar>(foo);

This cast will fail. Foo and Bar are incompatible types, unless atleast one of the following is true:

这个演员将会失败。Foo和Bar是不相容的类型,除非至少有一个是正确的:

  • Foo is derived from Bar, Or
  • Foo来自Bar或
  • Bar has a constructor that takes Foo, Or
  • Bar有一个带Foo的构造函数
  • Foo has a user-defined conversion to Bar.
  • Foo有一个用户定义的到Bar的转换。

The bigger question here is not whether it will cast successfully or not. The bigger and the actual question should be: what do you want to get out of such cast? Why would you want to do such a thing in the first place? What is it supposed to do? I mean, how would the Bar object be initialized from Foo object?

这里更大的问题不是它是否会成功投出。更重要和实际的问题应该是:你想从这样的演员阵容中得到什么?你为什么一开始就想做这样的事?它应该怎么做?我的意思是,如何从Foo对象初始化Bar对象?

The sensible way to convert one type to another is one of the following ways:

将一种类型转换为另一种类型的明智方法是以下方法之一:

Either define Foo as:

要么Foo定义为:

class Foo : public Bar
{
   //...
};

Or define Bar as:

或者酒吧定义为:

class Bar
{
  public: 
       Bar(const Foo &foo); //define this constructor in  Bar!
};

Or provide a conversion function in Foo as:

或在Foo中提供转换函数:

class Foo
{
  public: 
       operator Bar(); //provide a conversion function Foo to Bar!
};

#2


11  

Here's the rule:

这里的规则:

The expression static_cast<T>(e) is valid if and only if the following variable definition is valid

如果且仅当以下变量定义有效时,表达式static_cast (e)是有效的

T x(e);

where x is some invented variable.

x是某个虚构的变量。

So, in general, two unrelated types cannot be cast to one another. However, if one type is derived from the other, or when one defines a conversion function to the other, or has a constructor taking a single argument of the other type - in these cases static_cast will be well-defined.

因此,一般来说,两个不相关的类型不能相互转换。但是,如果一种类型是从另一种类型派生出来的,或者当一种类型定义到另一种类型的转换函数时,或者有构造函数使用另一种类型的单一参数时——在这些情况下,static_cast将得到良好的定义。

Does it ever make sense? For example, if T defines a constructor that takes a single U and the constructor is explicit then static_cast<T>(e) where e is of type U would make perfect sense

这有意义吗?例如,如果T定义了一个构造函数,它只接受一个U,构造函数是显式的,那么static_cast (e),其中e的类型是U,这是完全合理的

#1


10  

Bar bar = static_cast<Bar>(foo);

This cast will fail. Foo and Bar are incompatible types, unless atleast one of the following is true:

这个演员将会失败。Foo和Bar是不相容的类型,除非至少有一个是正确的:

  • Foo is derived from Bar, Or
  • Foo来自Bar或
  • Bar has a constructor that takes Foo, Or
  • Bar有一个带Foo的构造函数
  • Foo has a user-defined conversion to Bar.
  • Foo有一个用户定义的到Bar的转换。

The bigger question here is not whether it will cast successfully or not. The bigger and the actual question should be: what do you want to get out of such cast? Why would you want to do such a thing in the first place? What is it supposed to do? I mean, how would the Bar object be initialized from Foo object?

这里更大的问题不是它是否会成功投出。更重要和实际的问题应该是:你想从这样的演员阵容中得到什么?你为什么一开始就想做这样的事?它应该怎么做?我的意思是,如何从Foo对象初始化Bar对象?

The sensible way to convert one type to another is one of the following ways:

将一种类型转换为另一种类型的明智方法是以下方法之一:

Either define Foo as:

要么Foo定义为:

class Foo : public Bar
{
   //...
};

Or define Bar as:

或者酒吧定义为:

class Bar
{
  public: 
       Bar(const Foo &foo); //define this constructor in  Bar!
};

Or provide a conversion function in Foo as:

或在Foo中提供转换函数:

class Foo
{
  public: 
       operator Bar(); //provide a conversion function Foo to Bar!
};

#2


11  

Here's the rule:

这里的规则:

The expression static_cast<T>(e) is valid if and only if the following variable definition is valid

如果且仅当以下变量定义有效时,表达式static_cast (e)是有效的

T x(e);

where x is some invented variable.

x是某个虚构的变量。

So, in general, two unrelated types cannot be cast to one another. However, if one type is derived from the other, or when one defines a conversion function to the other, or has a constructor taking a single argument of the other type - in these cases static_cast will be well-defined.

因此,一般来说,两个不相关的类型不能相互转换。但是,如果一种类型是从另一种类型派生出来的,或者当一种类型定义到另一种类型的转换函数时,或者有构造函数使用另一种类型的单一参数时——在这些情况下,static_cast将得到良好的定义。

Does it ever make sense? For example, if T defines a constructor that takes a single U and the constructor is explicit then static_cast<T>(e) where e is of type U would make perfect sense

这有意义吗?例如,如果T定义了一个构造函数,它只接受一个U,构造函数是显式的,那么static_cast (e),其中e的类型是U,这是完全合理的