943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1495
  • C++ RSS
Nov 13th, 2008
0

Hiding an Assignment Operator

Expand Post »
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?
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Nov 13th, 2008
1

Re: Hiding an Assignment Operator

> 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.
c++ Syntax (Toggle Plain Text)
  1. struct window
  2. {
  3. // ...
  4. private:
  5. window( const window& ) ; // do not define this
  6. void operator= ( const window& ) ; // do not define this
  7. // ...
  8. }

see: http://www.boost.org/doc/libs/1_37_0...ss_noncopyable
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 13th, 2008
0

Re: Hiding an Assignment Operator

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
> 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.
c++ Syntax (Toggle Plain Text)
  1. struct window
  2. {
  3. // ...
  4. private:
  5. window( const window& ) ; // do not define this
  6. void operator= ( const window& ) ; // do not define this
  7. // ...
  8. }

see: http://www.boost.org/doc/libs/1_37_0...ss_noncopyable
Thanks! That makes things much clearer. I upped your rep but it doesn't look like you need it
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Nov 13th, 2008
0

Re: Hiding an Assignment Operator

useful, yes. common, not all that much.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
wt1099 is offline Offline
3 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Factorial problem
Next Thread in C++ Forum Timeline: Somebody Help me please!!! I'm new to c++ and I need help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC