A MyMethod (B &var) { A retVal; retVal.member = var.value + bar(var); return retVal; } |
valA = MyMethod(valB); |
A MyMethod (A &_hiddenArg, B &var) { A retVal; retVal.A::A(); // retVal的构造函数 retVal.member = var.value + bar(var); _hiddenArg.A::A(retVal); // A的复制构造函数 return; retVal.A::~A(); // retVal的析构函数 } |
A MyMethod(A &_hiddenArg, B &var) { _hiddenArg.A::A(); _hiddenArg.member = var.value + bar(var); Return } |
#include <stdio.h> class RVO { public: RVO(){printf("I am in constructor\n");} RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");} ~RVO(){printf ("I am in destructor\n");} int mem_var; }; RVO MyMethod (int i) { RVO rvo; rvo.mem_var = i; return (rvo); } int main() { RVO rvo; rvo=MyMethod(5); } |
I am in constructor I am in constructor I am in copy constructor I am in destructor I am in destructor I am in destructor |
I am in constructor I am in constructor I am in destructor I am in destructor |
#include <stdio.h> class A { public: A() {printf ("A: I am in constructor\n");i = 1;} ~A() { printf ("A: I am in destructor\n"); i = 0;} A(const A& a) {printf ("A: I am in copy constructor\n"); i = a.i;} int i, x, w; }; class B { public: A a; B() { printf ("B: I am in constructor\n");} ~B() { printf ("B: I am in destructor\n");} B(const B& b) { printf ("B: I am in copy constructor\n");} }; A MyMethod() { B* b = new B(); A a = b->a; delete b; return (a); } int main() { A a; a = MyMethod(); } |
A: I am in constructor A: I am in constructor B: I am in constructor A: I am in copy constructor B: I am in destructor A: I am in destructor A: I am in copy constructor A: I am in destructor A: I am in destructor A: I am in destructor |
A: I am in constructor A: I am in constructor B: I am in constructor A: I am in copy constructor B: I am in destructor A: I am in destructor A: I am in destructor A: I am in destructor |
//RVO类定义在sample1.cpp中 #include <stdio.h> RVO MyMethod (int i) { RVO rvo; rvo.mem_var = i; throw "I am throwing an exception!"; return (rvo); } int main() { RVO rvo; try { rvo=MyMethod(5); } catch (char* str) { printf ("I caught the exception\n"); } } |
I am in constructor I am in constructor I am in destructor I caught the exception I am in destructor |
I am in constructor I am in constructor I am in copy constructor I am in destructor I am in destructor I am in destructor |
I am in constructor I am in constructor I am in destructor I am in destructor |
#include <stdio.h> class RVO { public: RVO(){printf("I am in constructor\n");} RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");} int mem_var; }; RVO MyMethod (int i) { RVO rvo; rvo.mem_var = i; if (rvo.mem_var == 10) return (RVO()); return (rvo); } int main() { RVO rvo; rvo=MyMethod(5); } |
I am in constructor I am in constructor I am in copy constructor |
#include <stdio.h> class RVO { public: RVO(){printf("I am in constructor\n");} RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");} int mem_var; }; RVO MyMethod (int i) { RVO rvo; if (i==10) return (rvo); rvo.mem_var = i; return (rvo); } int main() { RVO rvo; rvo=MyMethod(5); } |
I am in constructor I am in constructor |
//RVO类定义在sample1.cpp中 #include <stdio.h> RVO MyMethod (int i) { RVO rvo; if (i==10) return (rvo); rvo.mem_var = i; return (rvo); } int main() { RVO rvo; rvo=MyMethod(5); } |
I am in constructor I am in constructor I am in copy constructor I am in destructor I am in destructor I am in destructor |
RVO MyMethod (int i) { RVO rvo; if (i!=10) rvo.mem_var = i; return(rvo); } |
#include <stdio.h> // RVO类定义在sample1.cpp中 RVO MyMethod (int i) { RVO rvo; __asm { mov eax,rvo //可以注释掉此行 mov rvo,eax //可以注释掉此行 } return (rvo); } int main() { RVO rvo; rvo=MyMethod(5); } |
I am in constructor I am in constructor I am in copy constructor I am in destructor I am in destructor I am in destructor |
#include <stdio.h> int NumConsCalls=0; int NumCpyConsCalls=0; class RVO { public: RVO(){NumConsCalls++;} RVO (const RVO& c_RVO) {NumCpyConsCalls++;} }; RVO MyMethod () { RVO rvo; return (rvo); } void main() { RVO rvo; rvo=MyMethod(); int Division = NumConsCalls / NumCpyConsCalls; printf ("Constructor calls / Copy constructor calls = %d\n",Division); } |