简体   繁体   English

在C ++中相同类型的向量中使用类的构造函数

[英]Using a class' constructor within a vector of the same type in C++

Consider: 考虑:

MyClass {
    //constructor
    MyClass()
    {}
.
.
.
};

Then, defining a vector of the same type 然后,定义相同类型的向量

int main()
{
.
.
    vector<MyClass>myVector(12);
.
.

Is this allowed? 可以吗?

for(int i = 0; i < myVector.size(); i++)
{
    //an attempt to fill my vector with initialized MyClass objects.
    myVector[i] = MyClass(); //Calling constructor
}

OR: (in the case of not defining a vector's size) 或:(在未定义向量大小的情况下)

for(int i = 0; i < whatever; i++)
{
    //an attempt to fill my vector with initialized MyClass objects.
    myVector.push_back(MyClass()); //Calling constructor
}

If this is not allowed, what is an alternative for initializing class instances and storing them in a vector without using pointers? 如果不允许这样做,初始化类实例并将其存储在向量中而不使用指针的替代方法是什么? Is there such a thing? 有这样的事吗?

Everything you wrote is acceptable. 您写的所有内容都是可以接受的。

vector<MyClass>myVector(12);

will call default constructor of MyClass 12 times. 将调用MyClass默认构造函数12次。 So this is equivalent to 所以这相当于

vector<MyClass>myVector;
for(int i = 0; i < 12; i++)
{
    myVector.push_back(MyClass());
}

Another variant you provided is slightly different 您提供的另一个变体略有不同

for(int i = 0; i < myVector.size(); i++)
{
    myVector[i] = MyClass();
}

Here every element of vector is already initialized somehow. 在这里,向量的每个元素都已经以某种方式初始化。 So when you do this, previous instance of MyClass will be removed (destructor will be called) and element will be assigned to new instance. 因此,当您执行此操作时,将删除MyClass先前实例(将调用析构函数),并将元素分配给新实例。

If you need make your default constructor private and initialize class with some value you have to use next aproach: 如果需要将默认构造函数设为私有并使用一些值初始化类,则必须使用下一个方法:

vector<MyClass>myVector(12, MyClass(some_value));

or as you already seen 或如你所见

vector<MyClass>myVector;
for(int i = 0; i < 12; i++)
{
    myVector.push_back(MyClass(some_value));
}

暂无
暂无

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

相关问题 c ++在同一个类的另一个构造函数中调用构造函数 - c++ call constructor within another constructor of the same class 如何在C ++中的同一类的构造函数中调用重载的构造函数? - How to call an overloaded constructor within a constructor of the same class in c++? C++ class 构造函数使用指向相同的指针 class object - C++ class constructor using a pointer to same class object C++ 代码 - 在同一 class 的构造函数中调用一个 class 的构造函数 - C++ code -calling a constructor of one class within a constructor of same class C++ 中向量的类内构造函数初始化 - In-class constructor initialisation of a vector in C++ 在类中使用相同的变量可以在构造函数C ++中使用吗? - Is using the same variable in a class ok to use in the constructor C++? 在 C++ 中的模板实例化中使用带有构造函数的类作为类型参数 - Using a class with a constructor as a type parameter in a template instantiation in c++ 在C ++中存储具有相同基类的对象 <vector> 按值(无指针)通过删除复制构造函数等 - Store objects with the same base class in C++ <vector> by value (no pointers) by deleting the copy constructor etc C ++向量列表初始化程序无法与我的课程的类型转换构造函数一起使用 - c++ vector list initializer not working with type converting constructor for my class C++ 在向量内修改 class object - C++ Modifying class object within a vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM