简体   繁体   English

将矢量数据从一个类复制到另一个类

[英]Copying Vector Data from one class to another

I am writing a C++ program in which multiple classes are declared I wanted to know how to fetch the content of a vector from one class to another class.我正在编写一个 C++ 程序,其中声明了多个类我想知道如何将向量的内容从一个类获取到另一个类。

void Registration::displayRegisteredCards()
{
    vector<Registration> listReg;
    Registration r1("Niketh", 1234, 10000);
    Registration r2("Parth", 5678, 5000);
    Registration r3("Shilpa", 2468, 15000); 
    Registration r4("Vijay", 1357, 20000); 
    listReg.push_back(r1);
    listReg.push_back(r2);
    listReg.push_back(r3);
    listReg.push_back(r4);
    int size = listReg.size();
} 

how to fetch the content of a vector from one class to another class如何将向量的内容从一个类获取到另一个类

You can access a member of a class using the member-of-object operator .您可以使用 member-of-object operator 访问类的成员. . .

struct class_a {
    std::vector member_vector_1;
};


struct class_b {
    std::vector member_vector_2;
};

void foo()
{
    class_a instance_a;
    class_b instance_b;
    instance_b.member_vector_2 = instance_a.member_vector_1;
    //        ^ member-of-object operator  ^
}

In this example, the vector member of one class instance was copy-assigned into a vector member of an instance of another class.在这个例子中,一个类实例的向量成员被复制分配到另一个类实例的向量成员中。

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

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