Why is my object not able to modify it's own vector of objects?

52 Views Asked by At

I have a "CConfiguration" class which has a protected vector of CConfigSection objects. I'm trying to implement a method ("testFn1") in CConfiguration that will iterate over all the CConfigSection objects and modify the name of each object. If I print out the modified name from within testFn1(), I verify that the name is correctly modified. But when I print out the names after calling testFn1() from my main.cpp, the name is not modified, it's still the original name. So it seems like I'm only modifying a copy of the objects and not the objects themselves, but I don't understand why or how to fix it.

Code is below....There's a lot more code but I tried to boil everything down to just the relevant parts.

Thank you!

Configuration.h:

class CConfiguration
{
public:
    CConfiguration();
    ~CConfiguration(void);

    CStatus testFn1(void);
    vector<CConfigSection> getSections(void) { return sections; }

protected:
    vector<CConfigSection> sections;
};

Configuration.cpp:

// Modify the name of each CConfigSection object in the vector
CStatus CConfiguration::testFn1(void) {  
for (int II = 0; II < sections.size(); II++) {
    sections[II].name = sections[II].name + "_ABC";
}
return CStatus::OK();

}

main.cpp:

vector<CConfigSection> sections = config.getSections(); // Get copy of sections
for (int II = 0; II < sections.size(); II++) {
    cout << "main: Name Before = " << sections[II].name << endl;
}

config.testFn1(); // Update name of all sections

sections = config.getSections(); // Get updated copy of sections w/ new name
for (int II = 0; II < sections.size(); II++) {
    cout << "main: Name After = " << sections[II].name << endl;
}
0

There are 0 best solutions below