public or private inheritance of boost::noncopyable?

L

lbonafide

Every example I see (including on boost::eek:rg) 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
 
A

Alan Johnson

Every example I see (including on boost::eek:rg) 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.
 
A

Alexander Dong Back Kim

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.

That was a great answer =)

cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top