Boost logo

Boost :

From: Yuriy Koblents-Mishke (yurakm_at_[hidden])
Date: 2007-03-03 18:29:03


1. Never though about enforcing derivation before, but yes, it looks
as it may be useful. Especially if the utility class does not increase
the size of the final objects.

2. The technique of using protected constructor and virtual
inheritance to stop derivation does not look compiler-specific,
limited to gcc 4.1.1. Just in case, I tested your codes under MinGW
gcc 3.4.2 and under MSVC++ 8. Yes, the utility stops derivation. The
following line is OK:

Foo foo;

but line

Goo goo;

does not compile.

3. If the final class is itself derived, using the utility involves
multiple inheritances. Fortunately, it looks as in this context it is
fine. Utility class does not have data members, and, any case,
"rhombic" inheritance is impossible.

4. I still do not see why you need protected destructor: is not enough
to have a protected constructor? Why is it bad to destroy an object of
a final class by pointer?

5. It probably would be better to put the utility in a nested
namespace under boost::. See, for example, noncopyable.hhp.

Sincerely,
Yura.

On 3/3/07, Manuel Fiorelli <manuel.fiorelli_at_[hidden]> wrote:
> Is there any interest in a small utility which let developers to define
> pseudo-final classes, meaning
> that it is not possible to instantiate objects belonging to derived classes.
>
> Unfortunately, I could test my tool only on a Linux platform with the
> compiler gcc 4.1.1 and I am not
> sure the technique I used is portable to other compilers.
>
> Usage --------------------------------------------------------------------//
> Suppose you want to define a final class Foo, then you
> have to write
>
> class Foo : BOOST_NON_DERIVABLE
> {
> // ....
> };
>
> Now, you try to subclass it
>
> class Goo : public Foo
> {
> // ...
> };
>
> When you use inheritance it is quite obvious, you want to instantiate
> objects of the classes involved in the hierarchy; thus,
> somewhere you will probably write
>
> Goo goo; // a variant or a member of type Goo
>
> But the compiler will produce an error like that
>
> utility/nonderivable.hpp: In constructor 'Goo::Goo()':
> utility/nonderivable.hpp:23: error:
> 'boost::nonderivable_helper::nonderivable_helper()' is protected
> main.cpp:14: error: within this context
> utility/nonderivable.hpp:30: error:
> 'boost::nonderivable_helper::~nonderivable_helper()' is protected
> main.cpp:14: error: within this context
> main.cpp:14: error: no matching function for call to 'Foo::Foo()'
> main.cpp:9: note: candidates are: Foo::Foo(int)
> main.cpp:8: note: Foo::Foo(const Foo&)
> main.cpp: In function 'int main()':
> main.cpp:26: note: synthesized method 'Goo::Goo()' first required here
> utility/nonderivable.hpp: In destructor 'Goo::~Goo()':
> utility/nonderivable.hpp:30: error:
> 'boost::nonderivable_helper::~nonderivable_helper()' is protected
> main.cpp:14: error: within this context
> main.cpp: In function 'int main()':
> main.cpp:26: note: synthesized method 'Goo::~Goo()' first required here
>
> Best regards,
> Manuel Fiorelli
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk