Выходят ли векторные объекты за пределы области видимости при присвоении переменной класса? [дубликат]C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Выходят ли векторные объекты за пределы области видимости при присвоении переменной класса? [дубликат]

Сообщение Anonymous »


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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»