0 c++ constructor derived-class
我有这个代码:
#include <stdio.h>
class A
{
public:
A() { printf("A::A()\n"); }
A(const A &a) { printf("A::A(A &a)\n"); }
A &operator=(const A &a) { printf("A::operator=\n"); }
};
class B : public A
{
public:
B() { printf("B:B()\n"); }
B(const A &a) : A(a) { printf("B::B(A &a)\n"); }
B &operator=(const B &b) { printf("B::operator=\n"); }
};
int
main(int argc, char *argv[])
{
printf(">> B b1\n");
B b1;
printf(">> b2 = b1\n");
B b2 = b1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么该行B b2 = b1不调用构造函数B::B(const A &a)而是调用A::A(const A &a)?如何告诉编译器这样做?
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |