0

I am a rookie of C++ and I am looking at how to improve the efficiency of my code using move constructor and move assignment. But I have a few questions not clear about the move semantics:

1.Do I have to declare move constructor and assignment myself? I see a lot of conditions on when a move constructor will be implicitly declared, but assume I declare the following

SomeClass(SomeClass && other) = default

Should I get a usable move constructor implicitly?

2.What does the implicitly declared move constructor do? Does it simply call the move constructor of its member classes one by one? And I assume the copy action is always performed for primitive types such as int.

3.Instead of moving instance variables one by one, why does not C++ simply move the class as a whole? For example, if I have a class

SomeClass {
private:
    Class1 member1;
    Class2 member2;
    Class2 member3;
}

then does C++ move Class1, Class2, Class3 one by one? It sounds a lot simpler if C++ move SomeClass pointer as a whole.

DiveIntoML
  • 2,347
  • 2
  • 20
  • 36
  • 1
    Yes to 1. and 2. Can you try to explain what exactly "move as a whole" means in your mind? ;) – Michael Kenzel Jul 18 '18 at 21:15
  • @MichaelKenzel I mean for example your class has 1000 instance variables, instead of changing the 1000 instance variables one by one, simply change the pointer of the class, like python and java do. – DiveIntoML Jul 18 '18 at 21:17
  • @DiveIntoML, are you suggesting that compile should do that for 1 member variable also? If not, what's the threshold for switching behaviors? Who decides what that threshold is? – R Sahu Jul 18 '18 at 21:21
  • @DiveIntoML How exactly do you imagine that "simply change the pointer of the class" would work? An object lives at a certain place in memory. That place has an address. You cannot simply change that address. The address identifies the object, not the other way around. – Michael Kenzel Jul 18 '18 at 21:26

0 Answers0