I've been trying to figure out how to reach and change an attribution of an instance from another class' instance in C++. Apperently I've made a copying process inbetween, I don't know how to solve this.
I have two classes, Woman and Man, each has one instance in main: Andy and John, respectively. Both have names, their names is setted by their own mutators. Then I've created a vector attribution called as list for both Andy and John, and I've enlisted them into one another's list.
Now I want to reach and change the name of them throughout one another's lists. In a nutshell:
push back John to Andy's list.
get John's name through Andy's list.
change John's name through Andy's list (this is supposed to affect not only the item's name in Andy's list, but also the name of John-himself).
As far as I know, the solution should do something with pass-by-ref, but I don't know how to do so, once the classes are included.
output:
name of persons Andy John
name of persons Andy John
name of persons AndySugars JohnWick \\name of the person in Andy's and John's lists
name of persons Andy John \\name of Andy and John after changing their name from one-another's lists.
main.cpp:
#include "Man.h"
int main()
{
Woman Andy;
Man John;
Andy.setName("Andy");
John.setName("John");
Andy.setList(John);
John.setList(Andy);
std::vector<Man> list_of_Andy = Andy.getList();
std::vector<Woman> list_of_John = John.getList();
std::cout << "name of persons in the lists " << list_of_Andy.at(0).getName() << " " << list_of_John.at(0).getName() << std::endl;
std::cout << "name of persons " << Andy.getName() << " " << John.getName() << std::endl;
list_of_Andy.at(0).setName("JohnWick");
list_of_John.at(0).setName("AndySugars");
std::cout << "name of persons in the lists " << list_of_Andy.at(0).getName() << " " << list_of_John.at(0).getName() << std::endl;
std::cout << "name of persons " << Andy.getName() << " " << John.getName() << std::endl;
int a;
std::cin >> a;
return 0;
}
classes.h:
#include<string>
#include<iostream>
#include<vector>
class Man;
class Woman;
class Man
{
public:
Man();
std::string name;
std::vector<Woman> list;
void setName(std::string nameIn);
std::string getName();
void setList(Woman manIn);
std::vector<Woman> getList();
};
class Woman
{
public:
Woman();
std::string name;
std::vector<Man> list;
void setName(std::string nameIn);
std::string getName();
void setList(Man manIn);
std::vector<Man> getList();
};
Woman::Woman()
{
name = "undeclared";
}
void Woman::setName(std::string nameIn)
{
name = nameIn;
}
std::string Woman::getName()
{
return name;
}
void Woman::setList(Man manIn)
{
list.push_back(manIn);
}
std::vector<Man> Woman::getList()
{
return list;
}
Man::Man()
{
name = "undeclared";
}
void Man::setName(std::string nameIn)
{
name = nameIn;
}
std::string Man::getName()
{
return name;
}
void Man::setList(Woman manIn)
{
list.push_back(manIn);
}
std::vector<Woman> Man::getList()
{
return list;
}
You seem to be on the right track, to reach the same object from 2 different places you need to use either references or pointers.
A std::vector<Man> list
is a container that owns the objects in it. So every man in the vector is a separate object owned by the vector. When you do list.push_back(John);
you are pushing a copy of john to the back of the vector.
To make it work the way you want you need to store pointers or references in the vector. A pointer doesn't own an object (except for smart pointers, but that's a different topic) it simply points to an existing object.
I went ahead and made your code work with minimal changes by making the lists (vectors) hold pointers. It would look like this.
#include<string>
#include<iostream>
#include<vector>
class Man;
class Woman;
class Man
{
public:
Man();
std::string name;
std::vector<Woman*> list;
void setName(std::string nameIn);
std::string getName();
void setList(Woman* manIn);
std::vector<Woman*> getList();
};
class Woman
{
public:
Woman();
std::string name;
std::vector<Man*> list;
void setName(std::string nameIn);
std::string getName();
void setList(Man* manIn);
std::vector<Man*> getList();
};
Woman::Woman()
{
name = "undeclared";
}
void Woman::setName(std::string nameIn)
{
name = nameIn;
}
std::string Woman::getName()
{
return name;
}
void Woman::setList(Man* manIn)
{
list.push_back(manIn);
}
std::vector<Man*> Woman::getList()
{
return list;
}
Man::Man()
{
name = "undeclared";
}
void Man::setName(std::string nameIn)
{
name = nameIn;
}
std::string Man::getName()
{
return name;
}
void Man::setList(Woman* manIn)
{
list.push_back(manIn);
}
std::vector<Woman*> Man::getList()
{
return list;
}
// main.cpp starts here!
#include "Man.h"
int main()
{
Woman Andy;
Man John;
Andy.setName("Andy");
John.setName("John");
Andy.setList(&John);
John.setList(&Andy);
std::vector<Man*> list_of_Andy = Andy.getList();
std::vector<Woman*> list_of_John = John.getList();
std::cout << "name of persons in the lists " << list_of_Andy.at(0)->getName() << " " << list_of_John.at(0)->getName() << std::endl;
std::cout << "name of persons " << Andy.getName() << " " << John.getName() << std::endl;
list_of_Andy.at(0)->setName("JohnWick");
list_of_John.at(0)->setName("AndySugars");
std::cout << "name of persons in the lists " << list_of_Andy.at(0)->getName() << " " << list_of_John.at(0)->getName() << std::endl;
std::cout << "name of persons " << Andy.getName() << " " << John.getName() << std::endl;
int a;
std::cin >> a;
return 0;
}
Take the time to read a good C++ book or look at some tutorials online to understand the basic features of the language. Pointers and references could be a good starting point.