使用move从vector构造对象

时间:2017-08-03 12:50:00

标签: c++ c++11 std move stdvector

我有这样的代码:

class MyClass {
public:
    MyClass(std::vector &vec):
        v(vec)
    {}
    MyClass(??);

private:
    std::vector v;
};

我想像MyClass(std::move(some_vector))这样构建我的课程 我该如何实现?

我可以将4行改为v(std::move(vec)),但由于危险,这是隐藏的动作。

编辑: 实施例

int main {
    std::vector<int> v = {1, 2}
    MyClass myobj(v); // this should be disallowed
    //MyClass myobj(std::move(v)) //this should be the only way
    //here v is empty
}

4 个答案:

答案 0 :(得分:4)

我只接受std::vector按值接受std::move内部

class MyClass {
public:
    MyClass(std::vector vec):
        v(std::move(vec))
    {}

private:
    std::vector v;
};

答案 1 :(得分:1)

仅启用移动构造函数

class MyClass {
public:
    MyClass(std::vector &&vec):
        v(std::move(vec))
    {}
    MyClass() = delete; //prevent default constructor if you wish
private:
    std::vector v;
};

答案 2 :(得分:0)

你可以这样做:

struct MyClass {
    // rvalue only
    MyClass(std::vector&& vec):
        v(std::move(vec))
    {}

    // lvalue
    MyClass(const std::vector&) = delete;

private:
    std::vector v;
};

任何调用左值版本的尝试都将失败,调用已删除的重载。

答案 3 :(得分:0)

#include <vector>
#include <iostream>
#include <string>
#include <typeinfo>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::to_string;

class Parse
{
private:
    string         m_str;
    vector<string> m_vec;
public:
    // Constructor 1/4 with all defaults
    Parse(){ 
        cout << "\ncreating class with all default values\n";
        m_str = "";
        m_vec.push_back("");    
    }

    // Constructor 2/4 with all cases used
    Parse  (string         &tmp_str,
            vector<string> tmp_vec):

            m_str          (tmp_str),
            m_vec          (tmp_vec)
    {
        cout << "Your vector contains " + to_string(m_str.size()) + " arguments\n";
    }

    // Constructor 3/4 with other contents given but not vector
    Parse  (string         &tmp_str): 
            m_str          (tmp_str)
    {
        m_vec.push_back("");
    }
    // Constructor 4/4 with only Vector given but not other contents
    Parse  (vector<string>   tmp_vec):
            m_vec           (tmp_vec)
    {
        m_str = "";
    }

    string get_str_var(){return m_str;}

    void classed_print_vector_strings()
    {
        for (string i : m_vec){ cout << i << " \n";}
    }

};



// rm ./class_vector; g++ class_vector.cpp -o class_vector -std=c++17; ./class_vector arg1 arg2 arg3

int main(int argc, char *argv[])
{
    // turn **argv to a vector
    vector<string> args(argv, argv + argc);
    // iterate from argv through argv+argc

    // initialize with default arguments.
    Parse tracker1;
    // initalize with all used arguments
    Parse tracker2(args[0], args);
    // initalize with only the vector
    Parse tracker3(args);
    // initalzie without the vector, but with another arg
    Parse tracker4(args[0]);

    cout << "\nTracker 1 ---------------------\n";
    tracker1.classed_print_vector_strings();
    cout << "\nTracker 2 ---------------------\n";
    tracker2.classed_print_vector_strings();
    cout << "\nTracker 3 ---------------------\n";
    tracker3.classed_print_vector_strings();
    cout << "\nTracker 4 ---------------------\n";
    tracker4.classed_print_vector_strings();


    return 0;
}

// rm ./class_vector; g++ class_vector.cpp -o class_vector -std=c++17; ./class_vector arg1 arg2 arg3

// This will show you how to create a class that will give 
// you the option to initilize the class with or without 
// the vector with other arguments present and/or not present. 

//  My Background. . .  
//  github.com/Radicalware  
//  Radicalware.net  
//  https://www.youtube.com/channel/UCivwmYxoOdDT3GmDnD0CfQA/playlists