Hiding an Assignment Operator

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Hiding an Assignment Operator

 
0
  #1
Nov 13th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Hiding an Assignment Operator

 
1
  #2
Nov 13th, 2008
> 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.
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Hiding an Assignment Operator

 
0
  #3
Nov 13th, 2008
Originally Posted by vijayan121 View Post
> 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.
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: wt1099 is an unknown quantity at this point 
Solved Threads: 1
wt1099 wt1099 is offline Offline
Newbie Poster

Re: Hiding an Assignment Operator

 
0
  #4
Nov 13th, 2008
useful, yes. common, not all that much.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC