DEV Community

Discussion on: C++ Copy Constructors

Collapse
 
mohammadmahdijavid profile image
MohammadMahdi Javid • Edited

Hello Tomislav,
Thanks for sharing great information.
I tried what you said about the default copy constructor, but the memory address of those objects are not the same I've commented those addresses in front of them. so it seems that the default copy ctor uses deep copy.
can you help me to figure out what is wrong?
thanks

Link to my code:
dev-to-uploads.s3.amazonaws.com/up...

Collapse
 
tomislavkraljic profile image
Tomislav Kraljic • Edited
    Student* s1 = new Student("Tommy", 23);
    std::cout << s1 << std::endl;

    Student* s2(s1);
    std::cout << s2 << std::endl;
Enter fullscreen mode Exit fullscreen mode

You want to create a new instance of Student with a pointer. The pointer points to a memory address when you create the first object. When you shallow copy it, it also copies the memory address of the first student.