在构造函数中设置变量失败了?

时间:2018-05-15 14:36:48

标签: c++ variables constructor

GetModel()函数应返回一个包含36个索引的模型,但它返回一个包含0个索引的模型。

编译后:0个错误,0个警告。

我知道这个论坛讨厌初学者,但请礼貌。

int main(int argc, char** argv) {
    Model model;
    for (int i = 0; i < 36; i++) {
        model.indices.push_back(i);
    }

    Class cl(model);
    std::cout << cl.GetModel().indices.size() << std::endl; // should output 36 but outputs 0
}

分类: [我认为在这个类的counstructor中是错误的]

class Class
{
public:
    Class(Model model){
        m_model = model;

        std::cout << model.indices.size() << std::endl; //output: 36
        std::cout << m_mesh.GetModel().indices.size() << std::endl; //output: 36
    }

    inline Model GetModel() { return m_model; } //m_model.indices.size() should be 36, but is 0

protected:
private:
    Model m_model;
};

模特课程:

class Model
{
public:
    Model(const std::vector<unsigned int>& indices) {
        this->indices = indices;
    }
    Model(){}

    std::vector<unsigned int> indices;

    Model(const Model& other) {}
    void operator = (const Model& other) {}

1 个答案:

答案 0 :(得分:1)

问题在于

Model(const Model& other) {}
void operator = (const Model& other) {}

这不会使他们做默认行为。

Model(const Model& other) {}

Model设置为indices默认初始化(零大小),而不是从other复制。

void operator = (const Model& other) {}

什么都不做。为了解决这个问题,只需删除它们,因为默认情况下该类是可复制的。