- #include <iostream>
- #include<cstring>
- #include<cstdio>
- using namespace std;
- class XYZ
- {
- public:
- char *p;
- XYZ(char *name,int l){
- p=new char[l+1];
- strcpy(p,name);
- }
- };
- int main() {
- XYZ obj1("str",3);
- printf("content of obj1.p is : %u\n",obj1.p);
- XYZ obj2=obj1;
- printf("content of obj2.p is : %u",obj2.p);
- }
So here content of member p of obj1 and obj2 is same which is base address of string str
Note that memory is allocated once and pointer to that memory is set for both objects. So default copy constructor only copying members value that is value of obj1.p ( which is address of string "str" ) is copied to obj2.p