0

For a lab, my instructor asked us to include an assignment operator with our class, but to hide it. Does this mean to declare it as private and just leave an empty implementation? Is this common/useful?

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by wt1099
1

> declare it as private and just leave an empty implementation?
ideally, declare it as private and do not define it.

> Is this common/useful?
useful, yes. common, not all that much.
if there is a class that you write (for example a window), and want its instances not to have value semantics, you need to suppress any compiler-generated copy constructor and assignment operators.

struct window
{
    // ...
    private:
       window( const window& ) ; // do not define this
       void operator= ( const window& ) ; // do not define this
    // ...
}

see: http://www.boost.org/doc/libs/1_37_0/libs/utility/utility.htm#Class_noncopyable

Votes + Comments
To the point and accurate
0

> declare it as private and just leave an empty implementation?
ideally, declare it as private and do not define it.

> Is this common/useful?
useful, yes. common, not all that much.
if there is a class that you write (for example a window), and want its instances not to have value semantics, you need to suppress any compiler-generated copy constructor and assignment operators.

struct window
{
    // ...
    private:
       window( const window& ) ; // do not define this
       void operator= ( const window& ) ; // do not define this
    // ...
}

see: http://www.boost.org/doc/libs/1_37_0/libs/utility/utility.htm#Class_noncopyable

Thanks! That makes things much clearer. I upped your rep but it doesn't look like you need it :P

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.