Vector in a structurally better approach? C ++

I am trying to include a vector in my structure.

Here is my structure:

struct Region { bool hasPoly; long size1; long size2; long size3; long size4; long size5; long size6; //Mesh* meshRef; // the mesh with the polygons for this region long meshRef; std::vector<int> PVS; } typedef Region; 

Is the vector in this declaration valid or would it make sense to make a pointer to the vector. In the case of a pointer to a vector, I need to select a new vector. How to do it?

Thanks!

Edit: The problem is that it is causing an error pointing to xmemory.h, a file included in the MSVC ++ platform.

  void construct(pointer _Ptr, _Ty&& _Val) { // construct object at _Ptr with value _Val ::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Ty>(_Val)); // this is the line } 

Interestingly, this does not happen if I select it outside the structure and just in the function that I use. Any ideas?

+6
source share
4 answers

You can write it like this without a typedef:

 struct Region { bool hasPoly; long size1; long size2; long size3; long size4; long size5; long size6; long meshRef; std::vector<int> PVS; }; // no typedef required 

To answer your questions:

Is the vector in this ad valid

Yes it is.

or it makes sense to do a pointer to a vector.

No, probably not. If you did, you would have to implement a copy constructor, an assignment operator, and a destructor for copy behavior. In the end, you will have the same thing, but it will be additional work and could potentially lead to errors.

In the case of a pointer to a vector, I need to select a new vector. How to do it?

You will need to implement the copy constructor, copy assignment operator, and destructor:

 // Copy constructor Region(const Region & rhs) : hasPoly(rhs.hasPoly), // ... copy other members just like hasPoly above, except for PVS below: PVS(new std::vector<int>(*rhs.PVS)) { } // Copy assignment operator Region & operator=(const Region & rhs) { if (this != &rhs) { hasPoly = rhs.hasPoly; // ... copy all fields like hasPoly above, except for PVS below: delete PVS; PVS = new std::vector<int>(*rhs.PVS); } return *this; } // Destructor Region::~Region() { delete PVS; } 

Bottom line: your code is fine. You do not need to change it.

EDIT: Commit the assignment operator: check the comparison with this and return * this.

+4
source

It makes sense to do this, and you don't need new anyway, unless you really want the alias to be a separate vector. Also, you don't need any typedef stuff.

+3
source

It depends on how you use it.

If you want to copy the vector and data when copying the Region structure, leave it as not a pointer.

If you do not want it to be copied, you will need some kind of pointer to the vector.

If you use a pointer to a vector, you must be very careful about the safety of highlight / free exceptions. If you cannot reach your distribution in a safe way, you will leave the potential for memory leak.

A couple of options:

  • Make sure the code that selects the vector (and uses Region ) also frees the vector and is an exception in itself. This will require the Region exist only within this area of ​​code.
    You can do this by simply highlighting the vector on the stack and passing it to the pointer in the Region . Then, make sure you never return a Region object above this stack frame.
  • In Region you can also use some kind of smart pointer -> vector.
+2
source

Vector is fine. Keep in mind that if you copy this structure, the vector will be copied with it. Thus, in code with certain performance limitations, process this structure in the same way as for any other expensive type.

In production code, some people would prefer to use the class keyword rather than the struct keyword to define this class, since the vector member makes it a non-POD. If you are the author of your own style guide, you have nothing to worry about.

typedef is wrong, but just write struct Region { stuff };

+1
source

Source: https://habr.com/ru/post/889836/


All Articles