与static_cast相关的查询 [英] Queries Related to static_cast

查看:58
本文介绍了与static_cast相关的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过以下代码了解static_cast和测试:



I was trying to understand static_cast and testing with the following code:

#include <iostream>

using namespace std;

class Base
{
public:
  virtual void Func()
  {
     cout<<"Base Func()"<<endl;
  }
};
 
class Derived : public Base
{
public:
  void Func()
  {
     cout<<"Derived Func()"<<endl;
  }
 
  void Func1()
  {
     cout<<"Derived Func1()"<<endl;
  }
};
 

int main(int argc, _TCHAR* argv[])
{
	Base* pB1 = new Base();
	Base* pB2 = new Derived();
	Derived* pD1;
 
	pD1 = static_cast<Derived*>(pB1);
	pD1->Func1();            //Should get run time error as the actual object is
							//of type Base and Base does not contain Func1();

	pD1 = static_cast<Derived*>(pB2);  //It is OK as expected
	pD1->Func1();
}</blockquote>





首次调用pD1-> Func1()时应该会产生运行时错误。但它运行成功,输出是:



衍生的Fuc1()

派生的Fuc1()



任何人都可以解释为什么我没有得到运行时错误。还请说明使用static_cast生成运行时错误。



It should generate a run time error while calling first pD1->Func1(). But it is running successfully and the out put is:

Derived Fuc1()
Derived Fuc1()

Can anyone explain why I didn't get a run time error. Also please demonstrate when run time errors are generated with using static_cast.

推荐答案

我可以看到为什么你希望它失败, Func1 如何调用该方法?



当你使用 static_cast 进行向下转换时,你实际上是在告诉编译器只相信我,我知道我在做什么并且因为你的示例中的 Func1 方法没有触及与实例相关的任何数据,即使它确实不应该工作(这是因为实例函数几乎只是静态函数,其中编译器将实例作为一个名为 this 的秘密第一个参数传递。



要检查这一点,您可以将 int 成员初始化为 0 添加到派生并在 Func1 中打印它的值,当 Func1 为ca时为一个 Derived 提供了一个内存的值,其中 int 本来就是派生实例。



你想做的不是使用 static_cast 并使用 dynamic_cast 启用运行时类型信息,这可能会导致它提前失败。



请记住,C ++会让你做任何你想做的事情,所以你需要小心,例如,你可以在你的代码中添加以下内容;

Well I can see why you would want it to fail, and Func1 doesn't exist on Base how can it be Ok to call that method?

The thing is when you do the down-cast using static_cast you're essentially telling the compiler "just trust me on this, I know what I am doing" and since the Func1 method in you example doesn't touch any data related to the instance it's going to "work" even though it really shouldn't (this is because instance functions are pretty much just static functions where the compiler pass the instance as a secret first parameter called this).

To check this you can add an int member initialized to 0 to Derived and print it's value in Func1, when Func1 is called for a Derived it will have a the value of the memory where the int would have been had it been a Derived instance.

What you want to do is not use static_cast and use dynamic_cast with Runtime Type Information enabled, that might cause it to fail earlier.

Remember that C++ will let you do whatever you want, so you need to be careful, for example, you could add the following to your code;
string s = *reinterpret_cast<string*>(pB1);



它将编译并运行。该字符串的行为将是未定义的。


And it would compile and run. Behaviour of that string will be undefined though.


请参阅 https:/ /msdn.microsoft.com/en-us/library/c36yw7x9.aspx [ ^ ]。


这篇关于与static_cast相关的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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