免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2034 | 回复: 2

[C++] The c++ programming languages 4th 的一个初始化问题 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2014-05-21 09:23 |显示全部楼层
在3.2.2 Abstract type这节里,定义了一个抽象类
class Container {
public:
virtualdouble& operator[](int) = 0;  // purevirtual function
virtualint size() const = 0;  // constmember function (§3.2.1.1)
virtual~Container() {}  // destructor(§3.2.1.2)
};

然后继承
class Vector_container : public Container {  // Vector_containerimplements Container
Vector v;
public:
Vector_container(int s) : v(s) { }  //Vector of s elements
~Vector_container(){}
double&operator[](int i) { return v[i]; }
intsize() const { return v.size(); }
};

然后使用继承类。
void g()
{
Vector_container vc {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
use(vc);
}

但是Vector_container能接受初始化列表吗?我看构造函数只有一个int参数,这个怎么理解哦?

论坛徽章:
0
发表于 2014-05-21 14:17 |显示全部楼层
没尝试过,LZ可以编译一下试试

论坛徽章:
0
发表于 2014-05-23 13:13 |显示全部楼层
因为此类重载了运算符[],去查查吧你。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP