![]() |
#2
rjsp2017-04-10 11:54
|
在派生类类型和基类类型之间不存在这样的转换。
书前面说把派生类的基类部分隐藏起来就能当基类使用了。
而且下面还给了例子,怎么又说在派生类类型和基类类型之间不存在这样的转换
Bulk_quote bulk;//派生类对象
Quote item(bulk);//调用Quote::Quote( const Quote& )构造函数
item = bulk ;//调用Quote::operator = (const Quote& )
只有本站会员才能查看附件,请 登录
下面的代码也没有编译错误啊

#include <iostream>
#include <string>
using namespace std;
class Base {
public:
Base() = default;
Base( int x ): a(x){ }
private:
int a;
};
class Derived :public Base {
public:
Derived(int x) : b(x) { }
private:
int b;
};
class Derived2 :public Base {
};
int main( )
{
Derived dObj(3) ;
Base bObj( dObj ) , cObj ;
bObj = dObj ;
cObj = dObj ;
return 0;
}
#include <string>
using namespace std;
class Base {
public:
Base() = default;
Base( int x ): a(x){ }
private:
int a;
};
class Derived :public Base {
public:
Derived(int x) : b(x) { }
private:
int b;
};
class Derived2 :public Base {
};
int main( )
{
Derived dObj(3) ;
Base bObj( dObj ) , cObj ;
bObj = dObj ;
cObj = dObj ;
return 0;
}