vendredi 22 juillet 2016

C++ - Changing value of Member of a class through member functions

I have a class that has a std::vector member used to store objects of an external class.

class Example {
  private:
    std::vector<OtherClass> list_of_things_;
}

class OtherClass {
  public:
    void ChangeName(std::string name);
  private:
    std::string name_;
}

Through my code I want to change some of the OtherClass objects stored in this list_of_things_, so I use two functions in my Example class:

std::vector<OtherClass> RetrieveObjects() {
  std::vector<OtherClass> result;
  std::vector<OtherClass>::iterator it;
  for (it = list_of_things_.begin(); it != list_of_things_.end(); ++it) {
    if (some condition is met) {
      result.push_back(*it);
    }
  }
}

And then in another function inside Example I call this like:

std::vector<OtherClass> objs = RetrieveObjects();
std::vector<OtherClass>::iterator it;
for (it = objs.begin(); it != objs.end(); ++it) {
  it->ChangeName("new name");
}

Now, this is in principle working, but only when I check the names from the objs variable, this does not change the objects inside list_of_things_ which is my main intention.

Have I actually do a copy of the objects instead of retrieving the same objects in list_of_things_? If that is so, why? Have I made some other mistake? Should I be using pointers? I'm new to C++ and still finding my way around.

Here you can find a test code that runs:

#include <vector>
#include <string>
#include <iostream>

class OtherClass {
  public:
    OtherClass(std::string s) : name_(s) {}
    std::string GetName();
    void ChangeName(std::string name);
  private:
    std::string name_;
};

std::string OtherClass::GetName() {
  return name_;
}

void OtherClass::ChangeName(std::string name) {
  name_ = name;
}

class Example {
  public:
    Example(std::vector<OtherClass> l) : list_of_things_(l) {}
    void ChangeNames();
    void WriteNames();
  protected:
    std::vector<OtherClass> RetrieveObjects();
  private:
    std::vector<OtherClass> list_of_things_;
};

std::vector<OtherClass> Example::RetrieveObjects() {
  std::vector<OtherClass> result;
  std::vector<OtherClass>::iterator it;
  for (it = list_of_things_.begin(); it != list_of_things_.end(); ++it) {
    if (it->GetName() == "Name") {
      result.push_back(*it);
    }
  }
  return result;
}

void Example::ChangeNames() {
  std::vector<OtherClass> objs = RetrieveObjects();
  std::vector<OtherClass>::iterator it;
  for (it = objs.begin(); it != objs.end(); ++it) {
    it->ChangeName("new name");
    std::cout << it->GetName() << std::endl;
  }
}

void Example::WriteNames() {
  std::vector<OtherClass>::iterator it;
  for (it = list_of_things_.begin(); it != list_of_things_.end(); ++it) {
    std::cout << it->GetName() << std::endl;
  }
}

int main() {
  OtherClass oc = OtherClass("Name");
  OtherClass oc2 = OtherClass("None");
  OtherClass oc3 = OtherClass("Name");

  std::vector<OtherClass> v = {oc, oc2, oc3};

  Example ex = Example(v);
  ex.ChangeNames();
  ex.WriteNames();
}

Thank you!

Aucun commentaire:

Enregistrer un commentaire