协议缓冲区 - 静态数组初始化。 [英] Protocol Buffer - Static array initialization.

查看:83
本文介绍了协议缓冲区 - 静态数组初始化。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我们的项目是使用VC ++(MFC)构建的,很少有文件声明结构,很少有整数和char的数组,它们是静态初始化的。我们几乎没有使用如下数据初始化的静态成员变量,

  char  *  class  :: m_psz [] = {
COL
FAC
MAS
NOL
SET
} ;

TYPE_20 class :: m_Defaults [] = {
{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 }
};





并且下面有几个typedef,

  typedef   unsigned   char  TYPE_20 [< span class =code-digit> 20 ]; 





我的要求是使用Protocol Buffer概念并实现以上。那么我如何编写* .proto文件(或模式)来声明整数的静态数组并初始化它的值。最后我需要生成* .cs(cshar文件)。



以下是我对PB的基本知识:

1)我创建了类,使用了可选,重复的概念。

2)创建了声明2d数组的代码。

3)如何初始化数组在声明时是否在C ++中完成?

下面是没有初始化的2d数组的PB模式代码(在静态时间)



重复DefaultsForTYPE_20 m_DefaultsForTYPE_20 = 1;

消息DefaultsForTYPE_20 {

可选字节m_DefaultsForTYPE_20 = 1;

}

但是我怎么能在这里初始化它?



请提供你的建议。



先谢谢。

解决方案

你的TYPE_20是一个带有20个字符的char数组的内存。所以你这样访问它:



  void  setDefaults( int  v)
{
for int i = 0 ; i< sizeof (TYPE_20); i ++)
{
m_Defaults [i] = v; // 对缓冲区的索引访问
}
}





最好通过调用这样的函数在运行时初始化数据。



请阅读 Pointers Usage in C ++ 并尝试一些代码来更好地理解C ++的内部概念。通过为您节省很多麻烦值得花时间。 ; - )


Hi All,

Our project is build using VC++(MFC) and few files have structures declared, few arrays of integers and char's and they are initialized statically. We have few static member variables initialized with data as below,

char* class::m_psz[] = {
   "COL",
   "FAC",
   "MAS",
   "NOL",
   "SET"
};
 
TYPE_20 class::m_Defaults[] = {
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}
};



And have few typedefs as below,

typedef unsigned char TYPE_20[20];



My requirement is to use Protocol Buffer concept and achieve the above. So How can i write the *.proto file (or the schema) to declare the static array of integers and initialize values to it. And finally i need to generate the *.cs (cshar file) out of it.

Below are my Basic knowladge on PB:
1) I created the classes, used "optional", "repeated" concepts.
2) created the code to declare 2d array.
3) how do I initialize the array at declaration time as it is done in C++?
Below is code of PB schema for 2d array without initialization(at static time)

repeated DefaultsForTYPE_20 m_DefaultsForTYPE_20 = 1;
message DefaultsForTYPE_20{
optional bytes m_DefaultsForTYPE_20 = 1;
}
but how can i initialize it here?

Please provide me your suggestions.

Thanks in Advance.

解决方案

your TYPE_20 is some memory with a char array of 20 chars. So you access it this way:

void setDefaults( int v)
{
for(int i = 0; i < sizeof(TYPE_20); i++ )
{
m_Defaults[i] = v;//indexed access to the buffer
}
}



It is better to initialize data at runtime by calling such function.

Please read the Pointers Usage in C++ and try some code to better understand the inner conecept of C++. It is worth the time by saving you a lot of headaches. ;-)


这篇关于协议缓冲区 - 静态数组初始化。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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