本文介绍了正确投射fstream读写成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有很多关于reinterpret_cast的主题的线,以及它是多么糟糕,我仍然困惑着避免它的最好的方法,特别是当处理从fstream的读和写功能时。所以,这里是我的困境...



假设我们有一个整数数组,我们要填充一些文件中的数据。

  std :: ifstream iFile(...); 

//假定这个数组的类型不是选择的问题
int * a = new int [100];

我们可以用几种不同的方式阅读:

  iFile.read((char *)a,sizeof(int)* 100); 
iFile.read(reinterpret_cast< char *>(a),sizeof(int)* 100);
iFile.read(static_cast< char *>(static_cast< void *>((a)),sizeof(int)* 100);


第一个(C风格)是过时的,我们在C ++中引入了新风格的转换,有很好的理由;第二个是不可移植的,



有没有其他选择,我应该怎么办?



编辑:



目标是实现代码尽可能便携和符合标准。

声明为 char * ,而是改为
  // int * a = new int [100]; 
char * a = new char [ 100];
iFile.read(a,100);





编辑:



好,我阅读了您的评论,在这种情况下:

  iFile.read(reinterpret_cast< char *>(a),sizeof (int)* 100); 

应该已经足够了。



个人会选择C风格的转换:

  iFile.read((char *)a,sizeof(int)* 100); 

这是因为我在这里看不到任何危险。一切似乎很好,即使使用C风格的演员!






最好还是更乏味的演员



定义此函数模板:

 模板< class To,class From> 
到any_cast(From v)
{
return static_cast< To>(static_cast< void *>(v));
}

然后使用它:

  //`From`类型将从函数参数中推断出来。 :-) 
iFile.read(any_cast< char *>(a),sizeof(int)* 100);

看起来不错吗?



any_cast 可用于从任何类型转换为任何类型!


Although there was a lot of lines written on the topic of reinterpret_cast, and how bad it is, I'm still puzzled with best way to avoid it, especially when dealing with functions like read and write from fstream. So, here is my dilemma...

Let's say we have an integer array that we want to fill with some data from a file.

std::ifstream iFile( ... );

// presume that the type of this array is not a matter of choice
int *a = new int[ 100 ];

We can read with a few different casts:

iFile.read( (char *)a, sizeof( int ) * 100 );
iFile.read( reinterpret_cast< char * >( a ), sizeof( int ) * 100 );
iFile.read( static_cast< char * >( static_cast< void * >( ( a ) ), sizeof( int ) * 100 );

The first one (C-style) is outdated and new style casts we're introduced in C++ for good reasons. The second one is unportable and offers no guarantees. The third one is tedious to write and spoils the fun.

Is there any alternative to this and how should I go about it?

EDIT:

The goal is to achieve code as portable and as standard-conforming as possible.

解决方案

Why don't you declare a as char* instead, like this:

//int *a = new int[100];
char *a = new char[100];
iFile.read(a, 100 );

No casting required now.


EDIT:

Okay, I read your comment and the commented line in your post. In that case:

iFile.read(reinterpret_cast<char*>(a), sizeof(int)*100);

should suffice.

However,I personally would choose C-style cast:

iFile.read((char*)a, sizeof(int)*100);

That is because I don't see any danger here. Everything seems fine even with C-Style cast!


Best yet less tedious cast

Define this function template:

template<class To, class From>
To any_cast(From v)
{
    return static_cast<To>(static_cast<void*>(v));
}

Then use it:

//`From` type will be inferred from the function argument. :-)
iFile.read(any_cast<char*>(a), sizeof(int)*100);

Looks good?

I think this any_cast can be used to cast from any-type to any-type!

这篇关于正确投射fstream读写成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 01:09
查看更多