#include <iostream>
using namespace std;
class A
{
public:
A() {cout << "A()" << endl;}
A(const A& a){ cout << "A(const A& a)" << endl;}
void print() const {cout << "print in A" << endl;}
};
class B : public A
{
public:
B() {cout << "B()" << endl;}
B(const B& a){ cout << "B(const A& a)" << endl;}
void print() const {cout << "print in B" << endl;}
};
void printByValue(A a)
{
a.print();
}
void printByRef(const A& a)
{
a.print();
}
int main(int argc, char* argv[])
{
A a;
printByValue(a);
printByRef(a);
B b;
printByValue(b);
printByRef(b);
a = b;
printByValue(a);
printByRef(a);
return 0;
}
结果为:
A()
A(const A& a)
print in A
print in A
A()
B()
A(const A& a)
print in A
print in A
A(const A& a)
print in A
print in A
注:函数参数直接传值和“=”赋值均会调用拷贝构造函数。
没有评论:
发表评论