读书人

[有关问题]私有继承用using声明来使

发布时间: 2012-06-05 13:54:06 作者: rapoo

[问题]私有继承,用using声明来使用基类方法
私有继承,通过使用using声明,让基类方法可见:

我的代码如下:

C/C++ code
#include <iostream>#include <valarray>class Child:private std::valarray<int>{private:public:    Child(): std::valarray<int>(52, 5) {}    using std::valarray<int>::sum;    using std::vararray<int>::operator[];};int main(){    Child obj;    std::cout << obj.sum() << std::endl;     std::cout << "The first element is " << obj[0] << std::endl;                                       return 0;}


如果只使用sum方法,程序可以顺利编译通过,但有了operator[]方法,以上代码无法编译通过,提示:
main.cpp:19:50: error: within this context
main.cpp:19:50: error: ‘std::valarray<int>’ is not an accessible base of ‘Child’

不解,望各位网友解答。 谢谢

[解决办法]
我觉得楼主还是自己重新实现下[]的比较好。

读书人网 >C++

热点推荐