Выходят ли векторные объекты за пределы области видимости при присвоении переменной класса? [дубликат] ⇐ C++
Выходят ли векторные объекты за пределы области видимости при присвоении переменной класса? [дубликат]
I'm confused about how scoping and memory persistence works with std::vectors. Consider the following:
#include #include class Entity { std::string name; } class MyClass { std::vector class_vec; MyClass() { std::vector local_vec(100); class_vec = local_vec; } } int main(argc, char* argv[]) { MyClass myclass; } If the vector elements are copy by value everything should be OK but if they use move for example, bad things can happen as local_vec will go out of scope. From what I understand local_vec is created on the stack and when the constructor is exited should be removed from it.
In the documentation it says "move is used when assigning vectors when possible" - isn't this asking for problems? When do I need to be worried about a vector assigned to another vector will have its elements go out of scope if they have been created in a certain scope?
A for sure way to cause a problem is if a vector of pointers are used to reference stack objects but that isn't being done here.
Источник: https://stackoverflow.com/questions/781 ... s-variable
I'm confused about how scoping and memory persistence works with std::vectors. Consider the following:
#include #include class Entity { std::string name; } class MyClass { std::vector class_vec; MyClass() { std::vector local_vec(100); class_vec = local_vec; } } int main(argc, char* argv[]) { MyClass myclass; } If the vector elements are copy by value everything should be OK but if they use move for example, bad things can happen as local_vec will go out of scope. From what I understand local_vec is created on the stack and when the constructor is exited should be removed from it.
In the documentation it says "move is used when assigning vectors when possible" - isn't this asking for problems? When do I need to be worried about a vector assigned to another vector will have its elements go out of scope if they have been created in a certain scope?
A for sure way to cause a problem is if a vector of pointers are used to reference stack objects but that isn't being done here.
Источник: https://stackoverflow.com/questions/781 ... s-variable
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение