简体   繁体   English

默认赋值运算符在实际中是如何实现的 stl

[英]how does the default assignment operator implemented in actual stl

suppose we are implementing a vector class and we do假设我们正在实现一个向量 class 并且我们这样做

vector v;矢量 v;

v={1,2,3,4}; v={1,2,3,4};

so,here we are implementing our own vector class so,how this list pass to assignment operator as an argument how we handle it because we passing constant which are not accessible by pointer variable and reference variable.所以,这里我们正在实现我们自己的向量 class 所以,这个列表如何作为参数传递给赋值运算符我们如何处理它,因为我们传递了指针变量和引用变量无法访问的常量。 But all this can be done in stl vector class, so how did they do?但是这一切都可以在stl vector class中完成,那么他们是怎么做到的呢?

Prior to C++17, all arguments to all functions name fully constructed objects.在 C++17 之前,所有 arguments 到所有函数名称完全构造的对象。

A prvalue std::initialiser_list<int> object (with no name) is constructed from {1,2,3,4} , and that is the constructor's argument.纯右值std::initialiser_list<int> object(无名称)由{1,2,3,4}构造,这是构造函数的参数。 That temporary object ceases to exist once v 's constructor ends (specifically at the end of the full expression that creates it).一旦v的构造函数结束(特别是在创建它的完整表达式的末尾),临时 object 就不再存在。

From C++17 onward, a value that will initialise an object can be passed around.从 C++17 开始,可以传递一个初始化 object 的值。 We still call it a prvalue std::initialiser_list<int> , but it isn't an object (yet).我们仍然称它为纯右值std::initialiser_list<int> ,但它不是 object(目前)。 Somewhere in v 's initialisation, where it gets used, a std::initialiser_list<int> object is constructed.v初始化的某个地方,它被使用的地方,构造了一个std::initialiser_list<int> object 。 It also ceases to exist after the constructor is done.它在构造函数完成后也不复存在。

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

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