JohnySiuu
JohnySiuu

Reputation: 156

All elements in a struct pointer vector seem to be the same as the last element C++

Why is every element in my struct* vector exactly the same as the last element that is pushed. Everything works its just when I go to push_back the struct object* but when I iterate through it after it always comes out as the last element that was pushed.

(Each Entity* Name Element Before Push)bot 0 bot 1 bot 2 bot 3 bot 4

(After Push And Then Iterating Through Vector)bot 4 bot 4 bot 4 bot 4 bot 4

[EDIT] I forgot I made the name member static, thanks everyone.

struct Entity {
    static std::string Name;
};

std::string Entity::Name;

struct INetworkHandler {
    const uintptr_t SvCheatBase = Memory::base + 0x55F73F4;
    static std::vector<Entity*> EntityList;

    void init(uintptr_t nAddr, uintptr_t hAddr) {
        DWORD incr = 0x564; 
        for (int j = 0; j < 20; j++) {
            std::vector<char> rList = {}; Entity* e = new Entity();
            for (int i = 0; i < 32; i++) {
                char rC = RPM<char, BYTE*>(Memory::hHand, (nAddr + 0xC + incr) + i);
                if (rC != NULL)
                    rList.push_back(rC);
                else
                    break;
            }
            if (rList.empty()) {
                break;
            }
            std::string fStr(rList.begin(), rList.end());
            incr = incr + 0x564; 
            e->Name = fStr;
            this->EntityList.push_back(e);
        }
    }

    void List() {
        for (int i = 0; i < EntityList.size(); i++) {
            Entity* e = EntityList.at(i);
            if (i == 0) {
                std::cout << "Name: " << e->Name << "\n";
            }
            
        }
    }
};

Upvotes: 1

Views: 90

Answers (1)

JohnySiuu
JohnySiuu

Reputation: 156

Make the Name member of the Entity struct non static.

Section $9.4.2/1 from the C++ Standard (2003) says,

A static data member is not part of the subobjects of a class. There is only one copy of a static data member shared by all the objects of the class.

Upvotes: 1

Related Questions