C ++中的引用和指针类型转换 [英] Reference and pointer typecasting in C++

查看:139
本文介绍了C ++中的引用和指针类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够对矢量< void *>进行类型转换。参考矢量< Foo *>而我无法对矢量< void *>进行类型转换矢量< Foo *>。我收到错误C2440:'reinterpret_cast':无法从'std :: vector< _Ty>'转换为'std :: vector< _Ty>'为什么?



我可以在没有编译器错误的情况下将void *强制转换为Foo *。

I am able to typecast reference of vector<void*> to reference of vector<Foo*> whereas I am unable to typecast vector<void*> to vector<Foo*>. I am getting the error C2440: 'reinterpret_cast' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>' why?

And I am able to typecast void* to Foo* without getting compiler error.

void* g =&foo;
reportFooVector2(reinterpret_cast<Foo*>(g));





以下是我的全部代码。





Below is my entire code.

#include "stdafx.h"
struct Foo
{
  string s;
  int i;
};


void reportFooVector( vector <Foo*> * pvf )
{
 
}


void reportFooVector1( vector <Foo*> pvf )
{
}

void reportFooVector2( Foo *pvf )
{
}


int main()
{
	struct Foo foo = {"foo",  5};
	struct Foo goo = {"goo", 10};
	void* g =&foo;
	reportFooVector2(reinterpret_cast<Foo*>(g));
	vector <void *> vf;
	vf.push_back(&foo);
	vf.push_back(&goo);
	reportFooVector1( reinterpret_cast< vector < Foo * >  >(vf));
	reportFooVector( reinterpret_cast< vector < Foo * > * >(&vf));
}



在上面的程序中我收到编译器错误C2440:'reinterpret_cast':无法从'std :: vector< _Ty>'转换为'调用行reportFooVector1时的std :: vector< _Ty>'(reinterpret_cast< vector< Foo *>>(vf));



你能不能请任何人告诉它的原因?



我尝试过:



我浏览了网但无法得到任何解释。


In the above program I am getting the compiler error C2440: 'reinterpret_cast' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>' when calling the line reportFooVector1( reinterpret_cast< vector < Foo * > >(vf));

Could you please anyone tell the reason for it?

What I have tried:

I browsed the net but could not get any explanation.

推荐答案

原因很简单: reinterpret_cast 仅适用于指针(这是简短的答案,对于详细的解释,请随时阅读文档: reinterpret_cast conversion - cppreference.com [ ^ ])。
The reason is simple: reinterpret_cast only works with pointers (this is the short answer, for the detailed one, feel free to read the documentation: reinterpret_cast conversion - cppreference.com[^]).


类型转换会导致奇怪的错误如果做错了,那么最好避免这种情况或者小心处理它。



所以避免类型转换为void *因为程序员和编译器正在丢失有关物体的珍贵信息。任何向上演员都会导致不同的功能处理,并且会有崩溃的危险。



通常它表示设计不佳或编码 bug 的。也许不是在你的例子中,而是你的真实代码。
Type casting can lead to strange errors when done wrong, so it is better to avoid this or handle it with greatest care.

So avoid type casting to void* because the programmer and the compiler are loosing precious informations about the objects. Any up cast is leading to different handling of functions and has so the danger of a crash.

Normally it is a sign of poor design or a coding bug. Maybe NOT in your example, but your real code.


我认为在这些情况下有用的一件事是使用类型定义。以下是我在这种情况下定义的几种类型:
One thing I find helpful in these situations is to use type definitions. Here are a couple types I would define in this situation :
typedef struct Foo * pFoo;
typedef std::vector<pFoo>  vecpfoo;

然后我会调整你的函数:

Then I would adjust your functions to this :

void reportFoo( int index, pFoo pf )
{
   cin << "Foo item: " << index << endl;
   cin << "Member s: " << pf->s << endl;
   cin << "Member i: " << pf->i << endl;
}

void reportFooVector( vecpfoo & vpf )
{
   // this should be written with an iterator loop
   int count = (int)vpf.size();
   for( int i; i < count; ++i )
       reportFoo( i+1, vpf[i] );
}

我会像这样重写主函数:

and I would rewrite the main function like this:

int main()
{
   struct Foo foo = { "foo", 5 };
   struct Foo goo = { "goo", 10 };
   vecpfoo vf;
   vf.push_back( &foo );
   vf.push_back( &goo );

   reportFooVector( vf );
}


这篇关于C ++中的引用和指针类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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