Move class data items (C ++)

I want to know if I am doing this correctly. I have a class containing some data:

class Foo {
// ...
  Type a_;
  Type b_;
  Type c_;
};

And another class that does something else, but is built using class Foo. So, I believe ctor looks like this:

class Bar {
  Type a_;
  Type b_;
  Type c_;
  AnotherType A_;
  AnotherType B_;
  // ...
public:
  typedef std::tuple<Type, Type, Type> Tuple;

  Bar(const Tuple&);
  Bar(Tuple&&);
};

Now I need to create a method Foothat will return a tuple of data members for which I need Bar, which I can pass to Barctor. I also make an rvalue reference for Tuple, because these data elements are class Foono longer needed except through class Bar, so why bother copying the data when I can move it?

, class Foo, a Tuple. , , Bar ctor, rvalue. ?

auto Foo::move_data() -> Tuple&& {
  return std::move( Tuple(a_, b_, c_) );
}

? ( - , . , typedefs .)

+4
2

, . :

Tuple&& Foo::move_data() {
    return std::move( Tuple(a_, b_, c_) );
}

Tuple, move Tuple... . , Tuple, :

Tuple Foo::move_data() {
    return Tuple(std::move(a_), std::move(b_), std::move(c_) );
}
+6

, , a, b c ?

class Abc {
    Type a_;
    Type b_;
    Type c_;
};

class Foo {
    // ...
    Abc abc_;
    int somethingNotInBar_;
};

class Bar {
    Abc abc_;
    AnotherType A_;
    AnotherType B_;
    // ...
public:
    Bar(const ABC&);
};

:

  • ;
  • ( , , );
  • ( , d, b).
0

All Articles