// Currency as pass-by-value argument void func(Currency p) { cout << "X = " << p.dollars(); } Currency bucks(100, 0); func(bucks); // bucks is copied into p
#include <iostream> #include <string> using namespace std; static int objectCount = 0; class HowMany { public: HowMany() { objectCount++; print("HowMany()"); } void print(const string& msg = "") { if (msg.size() != 0) { cout << msg << ": "; } cout << "objectCount = " << objectCount << endl; } ~HowMany() { objectCount--; print("~HowMany()"); } }; HowMany f(HowMany x) { cout << "begin of f " << endl; x.print("x argument inside f() "); cout << "end of f" << endl; return x; } int main() { HowMany h; h.print("after construction of h"); HowMany h2 = f(h); h.print("after call to f()"); }
#include <iostream> #include <string> using namespace std; static int objectCount = 0; class HowMany { public: HowMany() { objectCount++; print("HowMany()"); } HowMany(int i) { objectCount++; print("HowMany(int)"); } // 拷贝构造 HowMany(const HowMany& o) { objectCount++; print("HowMany(HM)"); } void print(const string& msg = "") { if (msg.size() != 0) { cout << msg << ": "; } cout << "objectCount = " << objectCount << endl; } ~HowMany() { objectCount--; print("~HowMany()"); } }; HowMany f(HowMany x) { cout << "begin of f " << endl; x.print("x argument inside f() "); cout << "end of f" << endl; return x; } int main() { HowMany h; h.print("after construction of h"); // 这种方式,是可以初始化 h2 的 HowMany h2 = 10; // HowMany h2 = h; HowMany h3 = f(h); h.print("after call to f()"); }
T::T(cosnt T&);
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; class Person { public: Person(const char *s); ~Person(); void print(); // private: char *name; }; Person::Person(const char *s) { name = new char[::strlen(s) + 1]; ::strcpy(name, s); } Person::~Person() { delete []name; } int main() { Person p1("John"); Person p2(p1); printf("p1.name = %p\n", p1.name); printf("p2.name = %p\n", p2.name); return 0; } // p1.name 和 p2.name 指向同一个地址,析构的时候,会析构两次; // 程序运行会报错。
Person copy constructor
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; class Person { public: Person(const char *s); // 自定义拷贝构造 Person(const Person& w); ~Person(); void print(); // private: char *name; }; Person::Person(const char *s) { name = new char[::strlen(s) + 1]; ::strcpy(name, s); } Person::Person(const Person& w) { name = new char[::strlen(w.name) + 1]; ::strcpy(name, w.name); } Person::~Person() { delete []name; } int main() { Person p1("John"); Person p2(p1); printf("p1.name = %p\n", p1.name); printf("p2.name = %p\n", p2.name); return 0; }
void roster(Person); // 函数声明 Person child("Tom"); // 创建对象 roster(child); // 调用函数,参数为对象本身
Person baby_a("Fred"); // 以下需要使用 copy constructor Person baby_b = baby_a; // not an assignment Person baby_c(baby_a); // not an assignment
Person f() { Person player("Musk"); return player; } int main() { Person p = f(); return 0; }
// 示例一: Person copy_func(char *who) { Person local(who); local.print(); return local; // copy constructor called! } // 示例二: Person nocopy_func(char *who) { return Person(who); // no copy needed! }
参考资料: