My question is how can i copy a list from one class to another?
I´d try something like:
list<int> GetList();
list<int> CSolid::GetList()
{
return list<int>(List);
}
And then
list<int> NewList = Class::GetList();
I would try this like this but im not sure if this is right. Are there better solutions ? (if this is wrong whats the right way to do it?
The good thing about using built in data structures in C++ is that mostly they work as you would want them when you use primitive types such as int/floats etc...
I'd probably use std::list pre-defined copy-constructor the following way:
class SomeClassWithIntList
{
public:
SomeClassWithIntList(const std::list<int>& _inputList = {})
{
m_NumberList = std::list<int>(_inputList);
}
void PrintList()
{
for(auto i : m_NumberList)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
std::list<int> GetList()
{
return m_NumberList;
}
private:
std::list<int> m_NumberList;
};
int main()
{
// Use class constructor to input a given list to the std::list<int>
// constructor
SomeClassWithIntList test1 = SomeClassWithIntList({1,2,3});
test1.PrintList();
// Here we created test2 with an empty list
SomeClassWithIntList test2 = SomeClassWithIntList();
test2.PrintList();
// We can use the constructor to copy the list
test2 = SomeClassWithIntList(test1.GetList());
test2.PrintList();
return 0;
}
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.