开发者

std::map - inserting a body that is a nested structure

Does the following insert work? The reason for my question is that the body has another structure in it that has yet another structure (array) in it. All the variables a, b, c, x, y, and z are secondary and are just there to support my question.

Thanks in advance.

struct S_A
{
    int a;
    float b;
    char c;
    // ...
    S_B my_double_nested_structure;
};

struct S_B
{
    int x;
    float y;
    char z;
    // .. .
    char array1[2];
};

typedef std::map<int, S_A> myAMapType;

S_A nestedStruct;
nestedStruct.a = 5;
nestedStruct.b = 5.9;
nestedStruct.c = 'A';
nestedStruct.my_double_nested_structure.x = 4;
nestedStruct.my_double_nested_structure.y = 8.9;
开发者_如何学JAVAnestedStruct.my_double_nested_structure.z = 'B';
nestedStruct.my_double_nested_structure.array1[0] = 'B';
nestedStruct.my_double_nested_structure.array1[1] = 'C';

main()
{
    myAMapType finalMap;
    finalMap.insert(std::pair<int, S_A>(3, nestedStruct);
}


Your code would compile and work if you define struct S_B before struct S_A because you put the member object of type S_B to S_A definition that implies that complete type S_B must be defined at that point (if it would be just pointer to object then incomplete type would be sufficient).

And you must put assignments

nestedStruct.a = 5;
nestedStruct.b = 5.9;
nestedStruct.c = 'A';
nestedStruct.my_double_nested_structure.x =4;
nestedStruct.my_double_nested_structure.y =8.9;
nestedStruct.my_double_nested_structure.z ='B';
nestedStruct.my_double_nested_structure.array1[0] ='B';
nestedStruct.my_double_nested_structure.array1[1] ='C';

into some function. Only declarations/definitions are allowed at global scope (not expression statements, etc).

In global scope you might use initialization list for your structure:

S_A nestedStruct = { 5, 5.9, 'A', { 4, 8.9, 'B', { 'B', 'C' } } };

Initialization lists are allowed for classes without explicit constructors (like your structures) and for arrays of such classes or arrays of simple types.


Yes. As long as there are no pointers this is all compiled to one monolithic object that can be copied around (e.g. into containers).

Also your code as is will not compile because of the forward reference to S_B, missing brackets etc.


This approach works (besides the typos and such in the code). Here is why:

The map copy-constructs the object which is copied, in your case S_A, into its internal data. Also S_A has a default (automatically generated) copy constructor which copies all its fields one-by-one and calls the copy constructor of S_B to copy the field my_double_nested_structure. It also has a default copy constructor which - in order - copies all its fields.

Hence, during the insert, all data is copied properly into the map.

Please remember that if those structs had any pointers, then the pointers themselves would be copied - not the objects they point to.


As others have noted, the code should work, except for typos. Normally I would use the []-operator to insert in a map though, e.g. finalMap[3]=nestedStruct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜