0

I am passing the Person object as a argument to the TimesTwo constructor and the PlusTen constructor. Both of these classes point to the same PersonObject. When I change the int age field in the PersonObj, The information return by TimesTwo and PlusTen should also change, but PlusTen is not returning the expected value.

#include <iostream>
using namespace std;

class PersonObj
{
    public:
    int age;

    PersonObj(int x)
    {
        age = x;    
    }

    int getAge()
    {
        return age;
    }  

    void setAge(int x)
    {
        age =x;
    }

};

class TimesTwo
{   
    public:
    PersonObj *person;

    TimesTwo(PersonObj p)
    {
        person = &p;
    }

    int getAgeTimesTwo()
    {
        return person->getAge() * 2;
    }
};

class PlusTen
{   
    public:
    PersonObj *person;

    PlusTen(PersonObj p)
    {
        person = &p;
    }

    int getAgePlusTen()
    {
        return person->getAge() + 10;
    }
};


int main()
{
    int num;

    PersonObj person(25);      
    TimesTwo t(person);         
    PlusTen p(person);

    num = t.getAgeTimesTwo();           
    cout << "Person's age * 2 = " << num <<endl;  
    num = p.getAgePlusTen();           
    cout << "Person's age + 10 = " << num <<endl;  

    person.setAge(28);          // Change/Set person's age to 26

    num = t.getAgeTimesTwo();           
    cout << "Person's age * 2 = " << num <<endl;  
    num = p.getAgePlusTen();           
    cout << "Person's age + 10 = " << num <<endl;  


    return 0;
}

Output:

Person's age * 2 = 50
Person's age + 10 = 4199934
Person's age * 2 = 56
Person's age + 10 = 4199934
  • 1
    You are holding on to dangling pointers. Undefined behavior. – R Sahu Jun 13 '17 at 04:16
  • Im not sure what that means. I'm more of a Java guy. – Juan Lantigua Jun 13 '17 at 04:21
  • You set `person` to point to the `p` parameter that was passed to the constructor. But that parameter ceases to exist as soon as the constructor returns. So you've set `person` to point to a `PersonObj` that no longer exists. – David Schwartz Jun 13 '17 at 04:21

1 Answers1

2

Your constructors for TimesTwo and PlusTen take in a PersonObj parameter by value, not by reference. You are setting the pointer to a temporary object that gets deallocated at the end of the constructor. To make sure that they point to the same object, you would need to pass PersonObj as a reference.

Instead of TimesTwo(PersonObj p), try using TimesTwo(PersonObj& p).

  • TimesTwo(PersonObj& p) { person = &p; } This looks so strange and almost redundant. So From the main TimesTwo is passed the value of PersonObj. Now into the TimesTwo constructor, it gets the '&' reference to PersonObj person. Then in the code of the constructor, the pointer is directed to the reference of the PersonObj instance. – Juan Lantigua Jun 13 '17 at 04:51
  • @JuanLantigua Or, alternatively, you can store the references to `PersonObj`, in `TimesTwo`/`PlusTen` classes, instead of pointers, since you are only setting them, in a constructor. – Algirdas Preidžius Jun 13 '17 at 05:12