简体   繁体   中英

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.

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 . .

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.

In object-oriented languages like C++ are several ways to access member variables of one class from another class. In the following links they are summarized How to access variables in different class from other class? , https://gamedev.stackexchange.com/questions/14217/when-several-classes-need-to-access-the-same-data-where-should-the-data-be-decl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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