1
class person
{
    private:
        string p_name;
        int p_age;
    public:
        person(string name, int age){
            p_name = name;
            p_age = age;
        }
        person(const person &pers){
            p_name = pers.p_name;
            p_age = pers.p_age;
        }
        void print(){
            cout << "Name: " << p_name << '\n';
            cout << "Age: " << p_age << '\n';
            cout << &p_age << '\n';
            cout << '\n';
        }
};
int main()
{
    person obj1{"Name", 18};
    obj1.print();
    person obj2{obj1};
    obj2.print();

    return 0;
}

Here's my code, it works, but when i delete copy constructor in still works, so what is a point to use copy constructor?

cigien
  • 57,834
  • 11
  • 73
  • 112

2 Answers2

0

In this example, there is absolutely no use. C++ automatically generates a copy constructor that does exactly what you just do here: Copies each constituent field.

You need to implement a copy constructor (and several other things) if your class is managing resources in some unusual way. Your person class is nice: It stores an int and a string by value, which is super predictable, so C++ does the right thing automatically. If you were storing raw pointers (say, an int*) then you'd need a copy constructor, as well as a copy assignment, move assignment, move constructor, and destructor.

But in modern C++, we try to avoid that. Instead of raw pointers, use references (int&) for borrowing data, unique pointers (std::unique_ptr<int>) for owning data, and shared pointers (std::shared_pointer<int>) for that rare situation where you need multi-ownership, and save raw pointers and custom Rule of Five methods for that incredibly rare situation where all of the above fail you.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
0

when i delete copy constructor in still works

You're not deleting the copy constructor. You're just not providing a user-defined copy constructor. Note that there is a difference between "deleting a copy ctor" and "not providing one". In the latter case, the compiler will synthesize the copy constructor for you.

That synthesized copy constructor, memberwise-copies the data members of its argument into the object being created. This means that the copy ctor that will be synthesized for your class person, will just copy(initialize) both of the data members p_name and p_age from the passed argument similar to what you were doing manually. Note that the synthesized copy ctor will intialize the data members instead of assigning values to them from the passed argument. For example, the synthesized copy ctor for your class person will be equivalent to:

person(const person& rhs): p_name(rhs.p_name), p_age(rhs.p_age)
{
    
}

what is a point to use copy constructor?

As i said, the synthesize copy ctor just memberwise copies the data members of the passed argument into the object being created. But sometimes we want to customize this behavior. That is, sometimes we want that the copy ctor will do something specific to our needs, for which we provide the user defined copy ctor. One example is when our class is managing resources in ways that the synthesized copy ctor will not be able to.

Jason
  • 36,170
  • 5
  • 26
  • 60