PDA

View Full Version : [C++] Implict copy constructor



dawwin
October 9th, 2010, 11:06 AM
I didn't found anywhere, what is behaviour of implict copy constructor in this case


class test1
{
string a;
string b;
};


Will it copy content of class test1 as normal bytes or call copy constructor of each element?

worksofcraft
October 9th, 2010, 11:35 AM
I didn't found anywhere, what is behaviour of implict copy constructor in this case


class test1
{
string a;
string b;
};


Will it copy content of class test1 as normal bytes or call copy constructor of each element?

When in doubt...
Try it out...


#include <string>
#include <stdio.h>
#include <iostream>

using namespace std;

struct my_string: string {
my_string() {}
my_string(const my_string &Original): string((string) Original) {
printf("copy constructing my_string\n");
}
my_string & operator=(const my_string &Original) {
printf("copy assignment my_string\n");
}
};

struct test1 {
my_string A;
my_string B;
};

int main() {
test1 One;
One.A += "One A";
One.B += "to B or not two B?";
test1 Two = One;
cout << "Two A=" << Two.A << endl;
cout << "Two B=" << Two.B << endl;
return !printf("hasta la vista\n");
}


p.s. You have to compile and run it to get an answer ;)

dawwin
October 9th, 2010, 11:46 AM
Thanks