复制构造函数:
A(const A &other){value=other.value;} 也就是传值参数改为常量引用。
#include<iostream> using namespace std; class A { private: int value; public: A(int n){value=n;} A(const A &other){value=other.value;} void print(){ cout<<value<<endl; } }; int main() { A a=10; A b=a; b.print(); return 0; }
时间: 2024-11-03 05:23:07