Search code examples
c++objectooppointersshared-ptr

why pointer variable inside private class can't point to outside variable of class


#include<iostream>
#include <string>

using namespace std;

class Human
{
    private:
        int *age;
        string *name;
    
    public:
        Human(string p_name, int value)
        {
            
            *name = p_name;
            *age = value; 
            cout <<"Name of Person is "<<*name <<" and age is "<<*age<<endl;
        }

        ~Human()
        {
            delete name;
            delete age;

            cout<<"Destructor release all memory So now name is "<<*name << " and age is "<<*age<<endl;
        }

        void display()
        {
            cout << "The name is "<<*name <<" and age is "<<*age<<endl;
        }
};

int main()
{
    int age = 24;
    string name = "CODY";
    Human cody(name,age);
    cody.display();
}

It is not printing anything.... What is happening can somebody explain it please.... Is it due to I implemented pointer variable incorrectly...Why is it incorrect please tell me then and what will be the alternative solution


Solution

  • why pointer variable inside private class can't point to outside variable of class

    The premise of your question is faulty. A private member variable can point to outside of the class.

        Human(string p_name, int value)
        {
            
            *name = p_name;
            *age = value;
    

    You didn't initialise the pointers, so indirecting through them results in undefined behaviour. Don't do this.

    cout << "The name is "<<*name <<" and age is "<<*age<<endl;
    

    Here, you indirect through the invalid pointers and the behaviour of the program is undefined.

            delete name;
            delete age;
    
            cout<<"Destructor release all memory So now name is "<<*name << " and age is "<<*age<<endl;
    

    Even if the pointers weren't invalid, it would be wrong to delete them. You didn't create any objects with allocating new, so there is nothing to delete. Deleting the pointers that weren't returned by allocating new will result in undefined behaviour. Don't do this.

    Furthermore, even if delete had been valid, that would invalidate the pointers, and the successive indirection through the newly invalidated pointers in the cout statement would result in undefined behaviour. Don't do this either.

    Solution: Remove the delete lines.


    A solution where the class points to variables outside of the class: In order to refer to an object outside of a function, you need to use indirection. You can use a pointer parameters for example, and initialise the members to point to the same objects:

    Human(string* p_name, int* value)
        : name(p_name), age(value)
        {}
    
    // example usage:
    Human cody(&name, &age);
    

    Note that storing pointers in members is precarious because you must be aware of the relative lifetimes of the pointed objects, and the object containing the pointers. You must be sure that the pointers don't outlive the pointed objects.

    Considering the problems of the referential design of your class, I urge you to consider using values instead. This would likely be more useful class in general:

    struct Human {
        std::string name;
        int age;
    
        void display()
        {
            std::cout
                << "The name is "
                << name
                << " and age is "
                << age
                << '\n';
        }
    };
    
    int main()
    {
        Human cody {
            .name="CODY",
            .age=24,
        };
        cody.display();
    }