std::list<T>.back() liefert ne Kopie?



  • Ich versteh das Szenario gerade nicht:

    #include <list>
    struct W_RX_PoolElement : public (C_Struct)
    	{
    int poolId;
    };
    typedef struct W_RX_PoolElement W_RX_PoolElement;
    
    W_RX_PoolElement pl;
    	pl.poolId = 9;
    
    	std::list<W_RX_PoolElement> x;
    	x.push_back(pl);
    
    	RM::W_RX_PoolElement nw = x.back();
    	nw.poolId = 11111;
    

    Dass pl sich von nw unterscheidet ist klar, aber wenn ich im Debugger x angucke, so hat das dortige Element die poolId 9 und nicht 11111.

    Woran kann das liegen?



  • Seikilos schrieb:

    #include <list>
    struct W_RX_PoolElement : public (C_Struct)
    	{
    int poolId;
    };
    
    W_RX_PoolElement pl;
    	pl.poolId = 9;
    
    	std::list<W_RX_PoolElement> x;
    	x.push_back(pl);
    
    	RM::W_RX_PoolElement nw = x.back(); //hier kopierst Du!
    	nw.poolId = 11111;
    

    Du erstellst ja auch eine neue Kopie. Was erwartest Du da? Du musst mw entweder als Referenz anlegen, oder Du arbeitest direkt auf x.back().



  • Ich idiot, alles richtig gemacht, bis auf das eine Zeichen am Ende...

    Schande auf mein Haupt

    Danke 🙂


Anmelden zum Antworten