简体   繁体   English

创建std :: vector <Class> 稍后调用Class构造函数

[英]Creating std::vector<Class> with calling Class constructor later on

I've been googling and stackoverflowing this question, but couldn't find any answer. 我一直在谷歌搜索和stackoverflowing这个问题,但找不到任何答案。

The thing I am trying to do is create a vector with a custom class, which I have no problem with. 我想做的事情是创建一个带有自定义类的向量,我对此没有任何问题。 I understand I need to do: 我了解我需要这样做:

std::vector<ExtClass> varName;

But, what I want to do is this: 但是,我要做的是:

Since varName is now of type ExtClass, I want to call it's constructor in another class. 由于varName现在是ExtClass类型,因此我想在另一个类中调用它的构造函数。 To clarify, I think the code will explain this the best: 为了澄清,我认为代码将最好地说明这一点:

class ExtClass {
    public:
        int ext;
        int pet;

    ExtClass();
};

ExtClass:ExtClass() {
    this->ext = std::rand();
    this->pet = std::rand();
}

________________________

class MainClass {
    public:
        std::vector<ExtClass> attribute;

    MainClass(int);
}

MainClass(int size) {
    this->attribute.reserve(size) // reserving enough places in vector
    for (int i = 0; i < size; ++i)
        this->attribute[i] = new ExtClass(); // problem
}

I have used all public members to avoid unnecessary code. 我使用了所有公共成员来避免不必要的代码。

I thought since std::vector attribute is a vector of ExtClass elements, and since none have been defined, that I could assign each element of the vector as: 我以为std :: vector属性是ExtClass元素的向量,并且由于尚未定义,因此我可以将向量的每个元素分配为:

new ExtClass()

and that would create, for example: 这样会创建例如:

attribute[0].ext = 5125;
attribute[0].pet = 151;

This is definitely not the case and the compiler argues on this line with: 绝对不是这种情况,编译器在以下方面争论不休:

note: candidate: constexpr ExtClass& ExtClass::operator=(const ExtClass&)
class ExtClass {
      ^~~~~~
note:   no known conversion for argument 1 from ‘ExtClass*’ to ‘const ExtClass&’
note: candidate: constexpr ExtClass& ExtClass::operator=(ExtClass&&)
note:   no known conversion for argument 1 from ‘ExtClass*’ to ‘ExtClass&&’

I am not an expert in C++, but relatively new to it and I am practicing. 我不是C ++的专家,但是相对较新,我正在练习。 Can you tell me what is my mistake? 你能告诉我我的错误是什么吗? What assumption did I make wrong? 我做错了什么假设?

Thanks! 谢谢!

Code as written can be fixed by this: 可以通过以下方式固定编写的代码:

MainClass(int size) 
   : attribute( size )
{
}

that's it it will initialize attribute with size default constructed objects of ExtClass . 就是这样,它将使用ExtClass默认size构造的对象初始化attribute

Note that this code: 注意这段代码:

this->attribute.reserve(size) // reserving enough places in vector
for (int i = 0; i < size; ++i)
    this->attribute[i] = new ExtClass(); // problem

even if we fix it to be compilable: 即使我们将其修复为可编译的:

this->attribute.reserve(size) // reserving enough places in vector
for (int i = 0; i < size; ++i)
    this->attribute[i] = ExtClass(); // problem

leads to UB as you try to access elements of empty vector - calling reserve does not change how many objects vector holds, only capacity for future use. 当您尝试访问空向量的元素时会导致UB-调用保留不会更改向量保持的对象数,只会更改将来使用的容量。

As the comments above indicate, you can replace: 如以上注释所示,您可以替换:

for (int i = 0; i < size; ++i)
    this->attribute[i] = new ExtClass();

with: 与:

for (int i = 0; i < size; ++i)
    this->attribute.emplace_back ();

Your vector will then manage the lifetimes of your ExtClass objects for you. 然后,向量将为您管理ExtClass对象的生命周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM