c++ - C++0x move constructor gotcha -


edit: I asked this question again (problems with this question are correct After doing)

The basic idea is that you will get some strange results while pointing to things that are worth running if you are not careful.


The C ++ driver constructor and the operator transferring can actually be used as positive things and in situations where copy creators make no sense because they do not require duplicate resources. is.

But in such cases they will bite you if you are not careful and it is particularly relevant as I have seen the proposals to allow the compiler to be the default implementation of the conductor. I will provide such links as someone can give me me.

So, here are some code that has some flaws which are not completely clear. I have tested the code to make sure that it is compiled in g ++ with the -std = gnuc ++ 0x flag what are the flaws and how will you fix them?

  #if (__cplusplus & lt; = 199711L) & amp; Amp; ! Define (__GXX_EXPERIMENTAL_CXX0X__) #Terror This C ++ 0x #endif #include & lt; Unordered_set & gt; # Include & lt; Vector & gt; # Include & lt; Utility & gt; # Include & lt; Algorithm & gt; Class Observer Interface {Public: Virtual ~ Observer Interface () {} Virtual Wide Observed () = 0; Virtual Zeros GoingAway () = 0; }; Class corporate {private: typed :: std :: unordered_set & lt; Inspector Interface * & gt; Obcontainer_t; Public: Overview () {} Overview (const observation & amp;) = Deletion; Const observation and operator = (const observation and amp; B) = delete; // g ++ does not currently help in the loss of the conductor builder (observation & amp; amp; amp; amp; amp; amp; B): observer _ (:: std :: move (b.observers_)) {} // g ++ The move does not support assignment const observation and operator = (observation & amp; amp; amp; amp; nbsp;); observers_ = :: std :: move (b.observers_); Return * This; } Virtual ~ Overview () {for (ii (observers_.begin)); I! = Observers_.end (); ++ i) {(* i) - & gt; GoingAway (); }} Void unObserve (Observer Interface * v) {auto folk (observers_.find (v)); If (loc! = Observers_.end ()) {observers_.erase (loc); }} Zero changed () (If! Observers_.empty ()) {// Copy unobserve work for supervisors :: std :: vector & lt; ObserverInterface * & gt; tmp; tmp.reserve (observers_.size) ()); Tmp.assign (observers_.begin), supervisors_.end ()); (Auto i (tmp.begin ()); i! = Tmp.end (); ++ i) {(* i) - & gt; Seen changed (); }}} Private: obcontainer_t observers_; }; Class supervisor: Public supervisor interface {public: observer () {} observer (conspector & amp;) = delete; Const Obspector and Operator = (const Obscure & amp; b) = Deletion; // g ++ currently does not help the conductor forget the manufacturer Observer (Observer & amp; amp; amp; nbsp; amp; B): Watched_ (B. Observed) {b.observed_ = 0; Return * This; } // g ++ does not currently support default move assignments. Const Obspector and Operator = (Observer & amp; B) {observed_ = b.observed_; B Observed_ = 0; Return * This; } Virtual ~ Observer () {If (observed_) {observed _-> United Nations (this); Observed_ = 0; }} Virtual Zero changed () {doStuffWith (observed_); } Virtual Zeros Celebrated GoingAway () {observed_ = 0; } Private: Celebrated * celebrated; // define somewhere else with doStuffWith (overview *); };  

There are many problems with the code.

  1. Observer :: Observed _ has been unlinked in the default constructor, which can lead to an undetermined behavior when the constructor is called.
  2. No value but 0 to supervisor :: saw_ , making variable redundancy
  3. Even if a supervisor involves a method to celebrate, you are not re-registering while transferring the supervisor.
  4. You are trying to return a value from the supervisor's conduction maker.
  5. The problem you are trying to solve has already been solved.
  6. This conferencing reference from non-assignment operators.

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -