Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

public or private inheritance of boost::noncopyable?

370 views
Skip to first unread message

lbon...@yahoo.com

unread,
Mar 27, 2008, 10:51:16 AM3/27/08
to
Every example I see (including on boost::org) of using noncopyable
involves inheriting from it privately. Is there a drawback from
inheriting publicly? Is it just that it's not a good example of and
"is-a" relationship?

#include <boost/utility.hpp>

class A : public boost::noncopyable {};

int main()
{
A a;

A b(a);

return 0;
}

g++ -Wall -I\boost_1_33_1 -c noncopy.cpp

noncopy.cpp: In copy constructor `A::A(const A&)':
/boost_1_33_1/boost/noncopyable.hpp:27: error:
`boost::noncopyable_::noncopyable
::noncopyable(const boost::noncopyable_::noncopyable&)' is private
noncopy.cpp:9: error: within this context


Alan Johnson

unread,
Mar 27, 2008, 11:57:12 AM3/27/08
to
lbon...@yahoo.com wrote:
> Every example I see (including on boost::org) of using noncopyable
> involves inheriting from it privately. Is there a drawback from
> inheriting publicly? Is it just that it's not a good example of and
> "is-a" relationship?

You've pretty much answered the question already. You don't want people
making a function like:

void profoundlyUselessFunction(const boost::noncopyable &);

The typical use of boost::noncopyable is a "implemented in terms of"
relationship, which is best modeled by inheriting privately.

From a technical standpoint, since everything interesting in
boost::noncopyable is private already, whether you inherit publicly or
privately makes very little difference.

--
Alan Johnson

Alexander Dong Back Kim

unread,
Mar 28, 2008, 2:28:02 AM3/28/08
to
On Mar 28, 1:57 am, Alan Johnson <aw...@yahoo.com> wrote:

That was a great answer =)

cheers,

0 new messages