public inheritance and mixin style



I read Scott Meyers Effective C++ book and I cant understand why did he use public inheritance in Item 49 (when using mixin-style base class)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<typename T>              // "mixin-style" base class for
class NewHandlerSupport{          // class-specific set_new_handler
    public:                           // support
    static std::new_handler set_new_handler(std::new_handler p) throw();
    static void * operator new(std::size_t size) throw(std::bad_alloc);
    ...                             // other versions of op. new —
                                    // see Item 52
    private:
    static std::new_handler currentHandler;
};
template<typename T>
std::new_handler NewHandlerSupport<T>::set_new_handler(std::new_handler p) throw()
{
    std::new_handler oldHandler = currentHandler;
    currentHandler = p;
    return oldHandler;
}
template<typename T>
void* NewHandlerSupport<T>::operator new(std::size_t size) throw(std::bad_alloc)
{
    NewHandlerHolder h(std::set_new_handler(currentHandler));
    return ::operator new(size);
}
// this initializes each currentHandler to null
template<typename T>
std::new_handler NewHandlerSupport<T>::currentHandler = 0;
class Widget: public NewHandlerSupport<Widget> {
    ...                          // as before, but without declarations for
};                               // set_new_handler or operator new 

if I follow Item 32 advice of this book that said "Make sure public inheritance models 'is-a'" I cant find "is-a"relationship between Widget class and NewHandlerSupport template class. I think this code should be implemented with private inheritance or composition of NewHandlerSupport into Widget but he used public inheritance.

for an example in Item 32 he said that because Student is a Person we can use:
1
2
class Person {...};
class Student: public Person {...};

but in another Item he said because Set(a collection of distinct objects) class is not a std::list (because list can have repeated object) we cant inherit Set publicly from std::list and he use composition of std::list in Set class.

In some designs I want to inherit both interface and implementation of a base class but I cant set "is-a" relationship between them so as this book said and i understand from it I should use composition or private inheritance because the relationship between them is "is-implemented-in-terms-of".

Anyone who read this book please help me.
Last edited on
Topic archived. No new replies allowed.