jeudi 30 mars 2017

std::move with && (rvalue reference) as a member function return type [duplicate]

I just want to initialize a member data oneObj of class Two as below which compiles fine. I would like to know whether it is advisable to use std::move and && together to initialize an object or just pass the oneObj as a reference to InitObjOne as follows InitObjOne(One & obj) and initialize it. is there any advantage over another. Please clarify.

Note: The InitObjOne need to perform few actions for creating an object of One, I've just given a skeleton of the functionalities here.

#include <iostream>
using namespace std;

class One
{
    public:
    int x;
};

class Two
{
   public:
   Two();

   private:
   One oneObj;
   One && InitObjOne();
};

Two::Two() : oneObj(InitObjOne())
{

}

One && Two::InitObjOne()
{
    One aObjOne;
    return std::move(aObjOne);
}

int main() 
{
    Two two;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire