Thread: Shallow Copying

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Shallow Copying


    I created a program that involves classes and objects. Two members of the classes are supposed to be numbers, one smaller than the other and one bigger than the other. Then there's functions inside the class that iterate through all the series of numbers in-between the two member classes.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class numbers {
    private:
    	int *begin;
    	int *end;
    public:
    	~numbers()
    	{
    		delete begin;
    		delete end;
    	}
    	void Beg(void)
    	{
    		begin = new int;
    		cout << "Name a small number" << endl;
    		cin >> *begin;
    	}
    	void End(void)
    	{
    		end = new int;
    		cout << "Name a big number" << endl;
    		cin >> *end;
    	}
    	void Increment()
    	{
    		int i;
    		for(i = *begin;i <= *end;i++)
    		{
    			cout << i << endl;
    		}
    	}
    	void Decrement()
    	{
    		int i;
    		for(i = *end;i >= *begin;i--)
    		{
    			cout << i << endl;
    		}
    	}
    };
    
    void Nums(numbers *x)
    {
    	x->Beg();
    	x->End();
    	x->Increment();
    }
    
    int main()
    {
    	numbers *nums = new numbers;
    	numbers *x = nums;
    	Nums(x);
    	getchar();
    	getchar();
    	return 0;
    }
    It works fine, but this is not what I want. The book I'm learning from is talking about shallow copying, where the compiler makes a copy of the classes members and assigns them to memory addresses instead of the actual values. I know this sounds unusual to ask but I want to know, what would I do in order for this program to error because of shallow copying? I want to know this for future problems just in case this happens again and I know what to do.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Admittedly, the example for shallow- vs deep-copying that I learned from involved strings (which back in those days were C-style strings).

    With shallow copying, you just copy all the fields in the source object to the destination object. That means that if a field is a pointer, then you copy that pointer to the destination object. The end-result is that both objects point to the exact-same data. The problem with this becomes apparent when one object changes that data and then the other object "unexpectedly" reads that changed data instead of the original.

    Example: the class points to a C-style string. In the original object, Obj1, that string says "original". You shallow-copy the original object to Obj2. Obj2's string, which is the same as Obj1's, says "original". Now you change Obj2's string to "modified". What does Obj1's string say, "original" or "modified"? It says "modified".

    In the deep copying example, when you copy Obj1 to Obj2, Obj2 allocates its own memory for that string and then it copies the contents of Obj1's string to Obj2's. Obj2 now has its own copy of that string. When you change Obj2's string and then read Obj1's string, you will find Obj1's string to still say "original".

    So the test is to copy the object and then change the value of what one object is pointing to and read the value of the other object.

    However, in your program I do not see where you are copying one object to another. As such, I do not see where shallow copying applies. You simply have two pointers to the same object. So use the first pointer to display the contents of the object, then use the second pointer to change the object and use the first pointer again to display the contents of the object. But that's just trivial.

IMN logo majestic logo threadwatch logo seochat tools logo