Re: deriving a class with a subset of base class methods?
Vladimir wrote:
On the first sight, you can derive your class from std::list. If this
inheritance will be public then you should declare functions in
private section to avoid use them
template< typename T>
class spec_list : public list<T>
{
void push_back(const T& _Val); // not accesible for spec_list user
Sorry, but that is wrong.
spec_list<int> sl;
// sl.push_back(42); // doesn't compile
list<int>& l = sl; // might happen while passing to a function!
l.push_back(23); // does happily compile
That's why you must not make the baseclass public unless you want to allow
access to all the baseclass' interfaces.
Simmilarly you can do the same thing with private inheritance and
write public wrappers around methods wich you can provide to user of
your list.
Way easier:
struct my_list: private list<int> {
using list<int>::iterator;
using list<int>::const_iterator;
using list<int>::begin;
using list<int>::end;
...
};
IOW, you simply import the desired interfaces with the 'using ..'
statements.
Nevertheless I think a better way is to use composition instead of
inheritance of STL containers. So write a class containing std::list
member and your set of appropriate public methods.
Well, private inheritance is not much different than composition, so yes, it
works just as well.
A good reading is 'C++ Coding Standards: 101 Rules...' by Sutter,
Alexandrescu, at least chapter about class design.
I second that, it's a great book.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Michael W??hrmann, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]