标签:
与C++中的构造函数相同,复制构造函数在对象生成过程中同样进行插入对应的Vtable虚表,但在成员变量赋值时,除具有复制构造函数的成员对象外,编译器合成复制构造函数其余均采用”Bitwise copy semantics(位逐次拷贝)”语义实现对象成员的拷贝,无论处于何种语境。测试代码如下:
#include <iostream> #include <string> #include <cstdlib> #include <cstring> using namespace std; class A { private: char *str; int len; public: A(const string &str) { this->str = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str,str.c_str(),len); cout << "A()" << endl; } A(const A&a) { if(this != &a) { if(this->str) delete this->str; string str(a.str); this->str = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str,str.c_str(),len); cout << "A(const A &a)" << endl; } } ~A() { delete str; str = NULL; cout << "~A()" << endl; } }; class B { private: char *str; int len; string str_; public: B(const string &str) { this->str = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str,str.c_str(),len); cout << "B()" << endl; } ~B() { delete str; cout << "~B()" << endl; } }; class C:public A { private: char *str_; int len; public: C(const string &str):A(str) { this->str_ = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str_,str.c_str(),len); cout << "C()" << endl; } ~C() { delete str_; cout << "~C()" << endl; } }; class D { private: char *str_; int len; public: D(const string &str) { this->str_ = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str_,str.c_str(),len); cout << "D()" << endl; } ~D() { delete str_; cout << "~D()" << endl; } virtual void fun() { cout << "D::fun()" << endl; } }; class E:public virtual D { private: char *str_; int len; public: E(const string &str):D(str) { this->str_ = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str_,str.c_str(),len); cout << "E()" << endl; } ~E() { delete str_; cout << "~E()" << endl; } virtual void fun() { cout << "E::fun()" << endl; } }; class F:public virtual D { private: char *str_; int len; public: F(const string &str):D(str) { this->str_ = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str_,str.c_str(),len); cout << "F()" << endl; } ~F() { delete str_; cout << "~F()" << endl; } virtual void fun() { cout << "F::fun()" << endl; } }; class G:public E,public F { private: char *str_; int len; public: G(const string &str):E(str),F(str),D(str) { this->str_ = (char *)(malloc(sizeof(char) * str.size())); len = str.size(); memcpy(this->str_,str.c_str(),len); cout << "G()" << endl; } ~G() { delete str_; cout << "~G()" << endl; } virtual void fun() { cout << "G::fun()" << endl; } }; void fun(D *d) { d->fun(); } //static A a("classA"); //static B b("classB"); //static C c("classC"); //static E e("classE"); static G g("classG"); int main(int argc, char *argv[]) { //A a1 = a; //B b1 = b; //C c1 = c; //E d1 = e; E e1 = g; F f1 = g; fun(&e1); fun(&f1); //fun(&g); return 0; }
上面分别测试单继承,多继承,无继承,多态各种情况下,g++编译器合成复制构造函数的语义执行情况,未提供复制构造函数类成员均为”Bitwise Copy Semantics”语义,多态特性不受影响。
D() E() F() G() E::fun() F::fun() ~F() ~D() ~E() ~D() ~G() ~F() ~E() ~D()
标签:
原文地址:http://www.cnblogs.com/maniford/p/4623984.html