0

i'm learning c++ and i'm experimenting with different scenarios.So i wanted to do a shallow copy a class so this is what i came up with -

#include <iostream>

class not_dynamic
{
private:
    int data;
public:
    not_dynamic(int temp_data = 200)                            //constructor
        :data(temp_data)
    {
        std::cout << "constructor initialized" << std::endl;
    }

    not_dynamic(const not_dynamic& source)
        :data(source.data)
    {
        std::cout << "copy constructor initialized" << std::endl;
    }
    void get_info()
    {
        std::cout << data << " " <<  &data << std::endl;
    }
};

int main()
{
    not_dynamic v{ 100 };
    v.get_info();
    not_dynamic jackie{ v };
    jackie.get_info();


}


for this i get the following result -

constructor initialized
100 00EFFBD4                    // 100 is the value in data and 00EFFBD4 is address of data
copy constructor initialized
100 00EFFBC8                    // same as above

but as i recall in shallow copy all the same instances of different object(both objects are same) should have same memory address, but in this case its same.

I'm really confused.Can you guys help me??

Thanks

default-303
  • 361
  • 1
  • 4
  • 15
  • 1
    “in shallow copy all the same instances … should have same memory address” — no, what makes you think that? – Konrad Rudolph Jun 26 '20 at 12:12
  • it seems like you misunderstood what a shallow copy is. For a type with an `int` member there is no difference between "shallow" and "deep" copy. One could even argue that it doesnt make sense to apply the terms in this context – 463035818_is_not_an_ai Jun 26 '20 at 12:16
  • You are confusing copying pointers with the address of copied objects. **Of course** two members of two different objects will have different addresses. But two pointers in two different objects may or may not have the same value (depending on whether a deep copy has been done or not). – john Jun 26 '20 at 12:23
  • C++ copies always by value. Shallow copy behavior emerges when you copy pointers or pointer-like objects. Copying a pointer results in two pointers that point to the same object. When those pointers are object members, they can share a state. But `int` is not a pointer and is not pointer-like so copying it never leads to the sharing of state. – François Andrieux Jun 26 '20 at 12:24
  • 1
    The distinction between deep and shallow copy only matters if at least one member of the `struct`/`class` type is a pointer or reference. If the only member of the class is of type `int`, there is no distinction between deep and shallow copy (they are the same). – Peter Jun 26 '20 at 12:32
  • @Peter so the concept of deep and shallow copy comes only when using pointers?? – default-303 Jun 26 '20 at 13:36
  • @default-303 - The consideration of doing deep versus shallow copy comes when there is indirection - for example, use of a pointer member that points at a variable. That means pointers, references, and (in a number of cases) classes with pointer or reference semantics (iterators, smart pointers, etc). – Peter Jun 27 '20 at 00:28

1 Answers1

0

The two objects are still different and distinct, and will have their own instances of the member variables each stored in different locations.

What you're doing with your shallow copy is basically equivalent to something like:

int a = 10;
int b = a;   // Shallow copy of a

The two variables a and b are still two different variables, even though the value of a have been copied into b. It's the value that is copied.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • okay then how is it different from dynamically allocating memory then assigning value of instance??If i dynamically allocate memory then both instances will have different memory address right??why is that? – default-303 Jun 26 '20 at 13:35