c++ - Initialising data structure at compile time -


I'm writing to create some initial data structures for an embedded application, but still in the design phase, Thinking how to start different data structures

In the examples given below, the size of the arrays will never change, but the content will be in the order of

from the data structures One such is:

  int id; Int type; Unsigned four data [x]   

for example (in C Notation)

  {4,1, {1,2,3,4,5 , 6,7,8}} {2,0, {4,2,1}} {3,2, {9,1,2 9, 2}} etc .. (about 20 other lines)   

The length of the above arrays will never change during the application, but the content will be

so I thought of a templated class:

  template & Lt; Int N & gt; Class DS {Public: // Total Element for Easy Preliminary IID; Int type; Unsigned four elements [N]; Fixed integer size () {return n; }};   

Then you can do just that:

  DS & lt; 8 & gt; V1 = {4,1, {1,2,3,4,5,6,7,8}}   

Problem: It is not easy to pass through methods without those methods. The template is being made, and it makes a lot of code bloat - the method is copied for each unique shape. I can use "et elements [1]" to structure it for similar structure and get data, but then I would not know the size, and this is also a bad way of doing this.

I do not have a function call like "doSomething (v1.id, v1.type, v1.elements, v1.size)

so I have left back to work things In the old way of doing:

  struct foo {int id; int type; unsigned char * data; integer shape;}; unsigned char data [] = {1, 2,3,4, 5,6,7,8}; foo bar = {4,1, data, size (data) / size (data [0])};   

Which works, And I can pass any method of 'Foo' around the structure.

But such a Is there a more cognitive C ++ method? Data and bar as two separate variables?

Some compilers accept the name of the field specification initialization:

  foo bar = {.id = 4, .type = 1, .data = {1,2 , 3,4,5,6,7,8}, .ize Unfortunately, Visual Studio 2010 still does not allow this syntax.  

If the data is continuous, then you prefix Want static const as Bleph This linker / Npailr will allow only the data section to read.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -