Hi,
I'm able to write a copy constructor for a class which contains pointer as a data member. However, when I have a reference as a data member, the copy constructor does not help, i.e. when I change that member for one object, the change is reflected in another one also.
Please refer to the code below.

class Student {
        private:
                int roll_no;
                char *pname;
                int & marks;

        public:
                Student():marks(a)      {
                        roll_no = 0;
                        pname = new char[7];
                        strcpy(pname, "no one");
                }

                Student(int a, char *p, int &m):marks(m)
                {
                        roll_no = a;
                        pname = new char [strlen(p) + 1];
                        strcpy(pname,
                }

                Student(const Student &ob): marks(ob.marks)
                {
                        cout << "TRACE: in copy con" << endl;
                        roll_no = ob.roll_no;
                        pname = new char[strlen(ob.pname)+1];
                        strcpy(pname, ob.pname);
                }
                void change_data(int num, char *str, int n)
                {
                        roll_no = num;
                        strcpy(pname,str);
                        marks = n;
                }
                void show_data();
};

int main()
{
        int a = 102;

        Student Ajay(1, "Ajay",a) ;
        Student s1 = Ajay;

        Ajay.show_data();       // 1 Ajay 102
        s1.show_data();         // 1 Ajay   102

        a = 1122;
        s1.change_data(12,"Roshan",a);

        Ajay.show_data();       // 1 Ajay 1122
        s1.show_data();         // 12 Roshan 1122 
        return 0;
}

now the question is, how to avoid this reflection of values in both the objects?

Storing marks in your class as an int rather than a reference to an int would be the simplest way of solving the problem.

Once assigned, a reference cannot be re-assigned and that is the cause of your problem here. In your copy constructor, you are creating a copy of the reference from the original object. So now, if the value of marks changes in the original object or the copy of it, the change will be reflected in both objects. Which is exactly what you're seeing.

Otherwise, I suppose you could try something like this in your copy constructor:

Student(const Student &ob): marks(*(new int (ob.marks)))
                {
                        cout << "TRACE: in copy con" << endl;
                        roll_no = ob.roll_no;
                        pname = new char[strlen(ob.pname)+1];
                        strcpy(pname, ob.pname);
                }

That code should assign marks to the address of a new int created using ob.marks.
But I'm not sure that would work, I've got a feeling that the new int will still be pointing at the same location as the original reference, leaving you back at square one.

Personally I'd say the easiest way of eliminating the problem would be to store marks as an int.

Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.