复制构造函数传引用

    技术2022-05-20  63

    #include "stdafx.h"

    #include <iostream>

     

    class A

    { private:  int value; public:  A(int n)    {    value = n;   }    A(const A &y)//此处只能传引用,传指针,传值都是要造成无限调用构造函数的。   {    value = y.value+10;   }    void Print()   {    std::cout << value << std::endl;   } };

    int main(int argc, char* argv[]){ A a = 10;  A b = a;  b.Print();//输出20 a.Print();//输出10 return 0;}


    最新回复(0)