动态转换为流数据 [英] dynamic cast for streamed data

查看:67
本文介绍了动态转换为流数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct A

{

int a;

int b;

virtual~A();

}


struct B:public A

{

int c;

}

我正在使用memcpy并通过

套接字调用在网络上传输结构。无视小端,大端问题,是否可以使用动态强制转换来计算我传输的结构。

struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.

推荐答案

Ninan写道:
Ninan wrote:
struct A
{
int a;
int b ;
虚拟~A();
}

结构B:公共A
{
int c;
}
我正在使用memcpy并通过
套接字调用在网络上传输结构。忽略小端,大端问题,是否可以使用动态强制转换来确定我传输的结构。
struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.




为什么你想要形象吗?发送一些唯一标识的前缀

发送的数据类型。


-

Salu2



Why do you want to figure things? Send some prefix that uniquely identifies
the type of data being sent.

--
Salu2


Ninan写道:
struct A
{
int a;
int b;
virtual~A() ;


结构B:public A
{
int c;
}
我正在使用memcpy并传输结构网络通过套接字调用。无论小端,大端问题,是否可以使用动态强制转换来确定我传输的结构。
struct A
{
int a;
int b;
virtual ~A();
}

struct B : public A
{
int c;
}
I am using memcpy and transmit the structure across the network via
socket calls. Disregarding the little endian , big endian issue, is it
possible to use dynamic cast to figure out which structure I
transmitted.




No. dynamic_cast使用RTTI来确定类型。那个

信息不会通过网络传输。


你不应该将memcpy用于具有虚函数的类

无论如何。你可能会遇到问题。更喜欢OO序列化

技术:

http://www.parashift.com/c++-faq-lit...alization.html


另请查看Boost序列化图书馆:

http:// boost。 org / libs / serialization / doc / index.html


干杯! --M



No. dynamic_cast uses RTTI to figure out what the type is. That
information is not transmitted across the network.

You shouldn''t be using memcpy with classes with virtual functions
anyway. You''ll likely run into problems. Prefer OO serialization
techniques:

http://www.parashift.com/c++-faq-lit...alization.html

Also check out the Boost serialization library:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M


这是一种可能性,我明确地想到了这一点。使用这种方法的问题

是在系统中更难做到

我正在使用


That is one possibility and I definitly thought about it. The problem
with that approach is that it is more difficult to do in the system
that I am working with


这篇关于动态转换为流数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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