从STL容器继承实现而不是委托可以吗?我有一个类来适应std:vectory来建模特定于域的对象的容器。我希望向用户公开大部分STD:VectorAPI,以便他/她可以使用熟悉的方法(大小、清除、at等)。以及容器上的标准算法。在我的设计中,这似乎是一种反复出现的模式:class MyContainer : public std::vector<MyObject>{public:
// Redeclare all container traits: value_type, iterator, etc...
// Domain-specific constructors
// (more useful to the user than std::vector ones...)
// Add a few domain-specific helper methods...
// Perhaps modify or hide a few methods (domain-related)};我知道在为实现重用类时更喜欢组合而不是继承的做法-但是一定会有限制的!如果我将所有内容委托给std:vectoral,那么就会有32个转发函数!所以我的问题是.。在这种情况下继承实现真的那么糟糕吗?风险有多大?有没有更安全的方法,我可以实现这一点,而不需要这么多的打字?我是一个使用实现继承的异端者吗?:)编辑:如何明确说明用户不应通过std:Vectoral<>指针使用MyContainer:// non_api_header_file.hnamespace detail{
typedef std::vector<MyObject> MyObjectBase;}// api_header_file.hclass MyContainer : public detail::MyObjectBase{
// ...};Boost库似乎一直在做这件事。编辑2:其中一项建议是使用免费功能。我将在这里显示为伪代码:typedef std::vector<MyObject> MyCollection;void specialCollectionInitializer(MyCollection& c, arguments...);result specialCollectionFunction(const MyCollection& c);etc...一种更OO的方法:typedef std::vector<MyObject> MyCollection;class MyCollectionWrapper{public:
// Constructor
MyCollectionWrapper(arguments...) {construct coll_}
// Access collection directly
MyCollection& collection() {return coll_;}
const MyCollection& collection() const {return coll_;}
// Special domain-related methods
result mySpecialMethod(arguments...);private:
MyCollection coll_;
// Other domain-specific member variables used
// in conjunction with the c
3 回答
![?](http://img1.sycdn.imooc.com/533e4c640001354402000200-100-100.jpg)
慕斯王
TA贡献1864条经验 获得超2个赞
template <typename Container>class readonly_container_facade {public: typedef typename Container::size_type size_type; typedef typename Container::const_iterator const_iterator; virtual ~readonly_container_facade() {} inline bool empty() const { return container.empty(); } inline const_iterator begin() const { return container.begin(); } inline const_iterator end() const { return container.end(); } inline size_type size() const { return container.size(); }protected: // hide to force inherited usage only readonly_container_facade() {}protected: // hide assignment by default readonly_container_facade(readonly_container_facade const& other): : container(other.container) {} readonly_container_facade& operator=(readonly_container_facade& other) { container = other.container; return *this; }protected: Container container;};template <typename Container>class writable_container_facade: public readable_container_facade<Container> {public: typedef typename Container::iterator iterator; writable_container_facade(writable_container_facade& other) readonly_container_facade(other) {} virtual ~writable_container_facade() {} inline iterator begin() { return container.begin(); } inline iterator end() { return container.end(); } writable_container_facade& operator=(writable_container_facade& other) { readable_container_facade<Container>::operator=(other); return *this; }};
- 3 回答
- 0 关注
- 588 浏览
添加回答
举报
0/150
提交
取消