【发布时间】:2014-09-19 03:07:24
【问题描述】:
我想将 B 类型的对象分配给 A 类型的对象,但我不知道为什么它适用于不同类型的分配?
#include <stdio.h>
class B
{
public:
B()
{
printf("B default constructor.\n");
}
};
class A
{
public:
A()
{
printf("A Default constructor.\n");
}
A(B const& b) // if add the tag "explicit" for the constructor, it will not work...
{
printf("User constructor.\n");
}
A(const A& a)
{
printf("copy-constructor.\n");
}
void get(){printf("A::get\n");}
};
int main()
{
A a = B(); // What's the meaning to assign object of type B to object of type A?
为什么它适用于上述行? 这样做是如何工作的?
a.get();
}
【问题讨论】:
标签: c++ object variable-assignment