+ 1
Searching by variable in objects of class
class A { Public: A(string n,int i):name(n),x(i); {} Int x; String name; Void print () { Cout<<x; } } Int main() { A.ob1("name1",1); A.ob2("name2",2) //Find and choose obj with name2 and use his function } I have a class A. And i have a lot of objects of this class "A.ob1("name1")" , "A.ob2("name2") etc for example. How perform a search by variable "string name" in all objects of class and choose this object? Its very simple in ue4 but i have no idea how to do that in c++.
1 Antwort
+ 1
Use data structure for example:
// a group of objects of class A
std::vector<A>objects;
// add object
objects.push_back(A("name1"));
// for loop in STL version
for(std::vector<A>::iterator it = objects.begin(); it != objects.end(); it++)
{
// search for the obj with string name == "name1"
if(it->name == "name1")
{
// do something
}
}