Boost logo

Boost :

From: Manuel Fiorelli (manuel.fiorelli_at_[hidden])
Date: 2007-03-03 11:29:02


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




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