[英]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.在这个例子中,一个类实例的向量成员被复制分配到另一个类实例的向量成员中。
In object-oriented languages like C++ are several ways to access member variables of one class from another class.在像 C++ 这样的面向对象语言中,有几种方法可以从另一个类访问一个类的成员变量。 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
, https://gamedev.stackexchange.com/questions/14217/when-several-classes-need-to-access-the-same-data-where-should-the-data-be-decl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.