reference:
- C++ Primer 5th Edition - 13.1 Copy,Assign,and Destory
- cppreference - Copy constructors
- cppreference - Copy assignment operator
main.cpp
#include "parent.h"
#define LOG(X) std::cout<<"[file:"<<__FILE__<<" line:"<<__LINE__<<"]-["<<__FUNCTION__<< "]"<< "\n"<<X<<"\n\n";
int main(int argc, char *argv[])
{
{
Parent parent("A");
Parent parent1(parent);
Parent parent2;
parent2 = parent;
LOG("parent.name:"<<parent._name <<" parent1.name:"<< parent1._name << " parent2.name:"<< parent2._name);
LOG("child.name:"<<parent._child->_name <<" child1.name:"<< parent1._child->_name << " child.name:"<< parent2._child->_name);
}
}
Defining classes that act like values.
To obtain the follwing output of function main, you could use the skills of copy-construtor and copy-assignment-operator about classes.
You need to be careful that the class Child is created by new, and will be deleted in the deconstructor function of class Parent.
If you don't handle it properly in the copy-constructor or copy-assignment-operator function of class Parent, the program you compile will crash easily because of double-delete its child obejct.
output of function main:
[file:..\CppLearningDemo\main.cpp line:10]-[main]
parent.name:A parent1.name:B parent2.name:C
[file:..\CppLearningDemo\main.cpp line:11]-[main]
child.name:A-child child1.name:B-child child.name:C-child
parent.h
#ifndef PARENT_H
#define PARENT_H
#include "child.h"
class Parent
{
public:
Parent()=default;
~Parent();
Parent(std::string name);
Parent(const Parent&);
Parent& operator=(const Parent&);
Child *_child;
std::string _name;
};
#endif // PARENT_H
parent.cpp
#include "parent.h"
#define LOG(X) std::cout<<"[file:"<<__FILE__<<" line:"<<__LINE__<<"]-["<<__FUNCTION__<< "]"<< "\n"<<X<<"\n\n";
Parent::Parent(std::string name):_name(name)
{
_child = new Child("A-child");
LOG("Parent constructor");
}
Parent::~Parent()
{
LOG("Parent destructor,do deleting child");
delete _child;
}
Parent::Parent(const Parent& parent)
{
LOG("Parent copy constructor");
_name = std::string("B");
_child = new Child(*parent._child);
}
Parent& Parent::operator=(const Parent& parent)
{
LOG("Parent copy assignment operator");
_name = std::string("C");
Child *child = new Child();
*child = *parent._child;
_child = child;
return *this;
}
child.h
#ifndef CHILD_H
#define CHILD_H
#include<iostream>
class Child
{
public:
Child()=default;
Child(std::string name);
~Child();
Child(const Child&);
Child& operator=(const Child&);
std::string _name;
};
#endif // CHILD_H
child.cpp
#include "child.h"
#define LOG(X) std::cout<<"[file:"<<__FILE__<<" line:"<<__LINE__<<"]-["<<__FUNCTION__<< "]"<< "\n"<<X<<"\n\n";
Child::Child(std::string name):_name(name)
{
LOG("Child constructor");
}
Child::~Child()
{
LOG("Child destructor");
}
Child::Child(const Child& child)
{
LOG("Child copy constructor");
_name = std::string("B-child");
}
Child& Child::operator=(const Child& child)
{
LOG("Child copy assignment construcetor");
_name = std::string("C-child");
return *this;
}
all output:
[file:..\CppLearningDemo\child.cpp line:5]-[Child::Child]
Child constructor
[file:..\CppLearningDemo\parent.cpp line:7]-[Parent::Parent]
Parent constructor
[file:..\CppLearningDemo\parent.cpp line:18]-[Parent::Parent]
Parent copy constructor
[file:..\CppLearningDemo\child.cpp line:15]-[Child::Child]
Child copy constructor
[file:..\CppLearningDemo\parent.cpp line:25]-[Parent::operator =]
Parent copy assignment operator
[file:..\CppLearningDemo\child.cpp line:21]-[Child::operator =]
Child copy assignment construcetor
[file:..\CppLearningDemo\main.cpp line:10]-[main]
parent.name:A parent1.name:B parent2.name:C
[file:..\CppLearningDemo\main.cpp line:11]-[main]
child.name:A-child child1.name:B-child child.name:C-child
[file:..\CppLearningDemo\parent.cpp line:12]-[Parent::~Parent]
Parent destructor,do deleting child
[file:..\CppLearningDemo\child.cpp line:10]-[Child::~Child]
Child destructor
[file:..\CppLearningDemo\parent.cpp line:12]-[Parent::~Parent]
Parent destructor,do deleting child
[file:..\CppLearningDemo\child.cpp line:10]-[Child::~Child]
Child destructor
[file:..\CppLearningDemo\parent.cpp line:12]-[Parent::~Parent]
Parent destructor,do deleting child
[file:..\CppLearningDemo\child.cpp line:10]-[Child::~Child]
Child destructor