lundi 27 novembre 2017

Can't modify queue of object in array

#include<iostream>
#include<deque>
struct testStr {
    std::deque<int> queue;
};

testStr arr[1] = {
    testStr()
};

testStr t = testStr();

void func() {
    testStr s = arr[0];
    s.queue.push_back(5);
    t.queue.push_back(6);
}

void func2() {
    std::cout << arr[0].queue.empty() << ' ';
    std::cout << t.queue.empty() << '\n';
}

int main() {
    func();
    func2();
    return 0;
}

The output is

1 0

I spent an hour tracking down this "bug" and don't understand why there's this behavior and how to overcome it. Appreciate any and all help here.

Aucun commentaire:

Enregistrer un commentaire