house(Person& first, Person& second)
one = first;
two = second;
cout << two.getMoney() << endl;
cout << jason.getMoney() << endl;
Ok, in this example it shows the correct money in two.getMoney() cout in the class function but once i do it after that in main the object (jason) still has the same money he had before, meaning it is only taking a copy of Jason and it's just going away when the class method is called. I know if I change the method to changeinfo(Person& , Person&) instead of getting them in the constructor this works and the real info is changed of the object.
What am I doing wrong? I would like to have this class to where I import these two into "house" class and then have methods that can change their real values instead of just a copied one that goes away. Thanks.
Lines 9 and 10 above are using the assignment operator to create copies, meaning the private variables 'one' and 'two' in lines 4 and 5 are really just copies of the original persons passed to the house constructor in line 22.
In order to hold a reference, you must declare reference variables like this:
But note that reference variables can only be initialized in constructor lists, meaning you must change the constrcutor to be like this:
house(Person &first, Person &second) : one(first), two(second)
Check out the C++ tutorial at this site or some other site to fully understand the topic.