Reputation:
Recently I came across following question. I could not figure out the reason.Could anyone point out me.
What is the parent class of “Buffered”? Why do you think this was chosen as the parent class? What is the main limitation to using this parent class?
Sample Code
template <typename T>
class Buffered : private boost::noncopyable
{
public:
explicit Buffered (const T& value = T())
: current_ (value), next_ (value) {}
virtual ~Buffered() {}
const T& get() const {
return current_;
}
void set (const T& value) {
next_ = value;
}
void skip() {
this->set(this->get());
}
void force(const T& value) {
next_ = current_ = value;
}
void flip() {
current_ = next_;
}
private:
T current_;
T next_;
};
Upvotes: 0
Views: 147
Reputation: 28883
In C++11, you can also say something like
class Class {
Class (Class const & other) = delete;
};
Then you get a compile-time error, rather than a linker error if you try to access it with member functions / friend functions.
Upvotes: 2
Reputation: 206566
If you don't want your users to be able to create any instances of your class(Buffered
in this case) through copying you derived your class from boost::noncopyable.
In short it makes your class non-copyable.
The boost doccumentation states the purpose clearly:
// Private copy constructor and copy assignment ensure classes derived from
// class noncopyable cannot be copied.
If you are not using boost, then you can make your class non-copyable by:
private
& The first gives you a compilation error if someone tries to copy your class instance while,
The second guards you against copying indirectly through friend
s by giving an linking error.
Upvotes: 3