Basics

 

Value Type

  • L-Value: 나중에 다시 부를 수 있는 변수 (Callable)
  • R-Value: 한번 쓰고 다시 안쓰는 변수 (Temporary)
    std::string a = "abc";      # L = R
    std::string b = a           # L = L 
    

파라미터 해석

  • 파라미터로 &가 오면 L-Value, &&가 오면 R-Value
  • L-Value는 copy sementic
  • R-Value는 move sementic
    void push_back( const T& value ) 
    void push_back( T&& value );
    

std::move() 소유권

move_sementic

  • std::move() 사용시 소유권이 넘어감
    string a = "abc";      // L = R
    string b = a           // L = L 
    string b = std::move(a);   // 추후에 a를 프린트해도 값이 안나옴
    

Rule of Three(Five)

  • Copy Consturctor
  • Copy Assignment
  • Desturctor
  • Move Constructor
  • Move Assignment
    class Person 
    {
      public:
          # Consturctor
          Person(string name, int age)
              :name(std::move(name)), age(age) {};
    
          # Copy Assignment
          Person(const Person& other) 
              :name(other.name), age(other.age) {};
    
          # Move Constructor
          Person(Person&& other)
              :name(std::move(other.name)), age(other.age) {};
    
      private:
          string name;
          int age;
    };
    

const 함수

  • 클래스의 멤버 변수를 전혀 바꾸지 않는다면 뒤에 const 붙일 것
    class AAA 
    { 
      public: 
          void printName() const;
            
          # name을 리턴하는 곳에서도 멤버 변수가 
          # 치환이 안되도록 const 앞에 붙여줄 것
          const string& getName() 
    
      private:
          string name;
    }