C2440 static_cast无法从基类转换为派生类 [英] C2440 static_cast cannot convert from base class to derived class

查看:555
本文介绍了C2440 static_cast无法从基类转换为派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么使用指针从基类强制转换为派生类的编译效果很好,但是使用非指针对象进行强制转换会产生错误C2440.

I am attempting to understand why casting from a base class to an derived class using pointers compiles fine, but casting using non-pointer object produces the error C2440.

下面我有一个由类GPSMessage继承的基类ThreadedMessage.

Below I have a base class ThreadedMessage that is inherited by class GPSMessage.

struct ThreadedMessage
{
  ThreadedMessage()
    : m_Type(0), m_ID(0)
  { }

  ThreadedMessage(uint Type, uint ID = 0) :
    m_Type(Type), m_ID(ID)
  { }

  uint m_Type;
  uint m_ID;
};    

struct GPSMessage : public ThreadedMessage
{
  GPSMessage()
    : ThreadedMessage()
  { }

  GPSMessage(double lat, double lon)
    : ThreadedMessage(1), m_lat(lat), m_lon(lon)
  { }

  double m_lat;
  double m_lon;
};

myFunction中,我试图从基类转换为派生类.

In myFunction I am attempting to cast from the base class to the derived class.

void myFunction(const ThreadedMessage& msg)
{
  const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine
  const GPSMessage message1 = static_cast<const GPSMessage>(msg);   // Error C2440
}

myFunction()的调用如下:

GPSMessage msg(21.123, 12.321);
myFunction(msg);

编译时,指针的转换可以正常编译,但是非指针的转换失败,并出现以下错误:

When I compile, the casting of a pointer compiles fine, but the non-pointer casting fails with the following error:

错误C2440:"static_cast":无法从"const ThreadedMessage"转换为"const GPSMessage".没有构造函数可以采用源类型,或者构造函数重载解析度不明确

error C2440: 'static_cast' : cannot convert from 'const ThreadedMessage' to 'const GPSMessage' No constructor could take the source type, or constructor overload resolution was ambiguous

为什么我不能使用非指针变量将基类转换为派生类?

Why am I unable to cast from the base class to the derived class with non-pointer variables?

编译器是MS VS 2008 C ++.

Compiler is MS VS 2008 C++.

是的,我看过其他类似的SO问题,但是我看过的问题似乎并不能回答我的问题.

Yes, I have looked at other similar SO questions, but the ones I have read don't seem to answer my question.

推荐答案

这两个强制转换具有不同的含义.

These two casts have differing meaning.

第一个演员

const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine

这意味着msg实际上是GPSMessage(或派生的)对象.您要求编译器威胁msg作为GPSMessage.将不会创建新对象,message将指向msg.如果msg实际上不是GPSMessage(或派生的),则此强制转换具有未定义的行为.

This means that msg is actually a GPSMessage (or a derived) object. You ask the compiler to threat msg as a GPSMessage. No new object will be created, message will point to msg. If msg is not actually a GPSMessage (or derived), then this cast has Undefined Behavior.

顺便说一句,以下强制转换具有相同的含义(广播至参考文献):

Btw, the following cast have the same meaning (casting to a reference):

const GPSMessage& message = static_cast<const GPSMessage&>(msg); // same meaning, which results in a reference instead of a pointer

关于第二个演员表:

const GPSMessage message1 = static_cast<const GPSMessage>(msg);   // Error C2440

这意味着您从msg创建一个 new 对象message1.您应该提供一种使这种转换成为可能的方法.例如,您应该为GPSMessage创建一个具有ThreadedMessage参数的构造函数:

This means, that you create a new object, message1 from msg. You should give a way to make this conversion possible. For example, you should create a constructor for GPSMessage which has a ThreadedMessage parameter:

struct GPSMessage {
    GPSMessage(const ThreadedMessage &); // needed constructor
};

或为ThreadedMessageGPSMessage创建转换运算符:

Or create a conversion operator for ThreadedMessage to GPSMessage:

struct ThreadedMessage {
    operator GPSMessage(); // conversion operator
};

这篇关于C2440 static_cast无法从基类转换为派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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