What you need to understand is the scope of variables and how to pass through reference or value depending on what you need.
In your example, the function should probably take the vector as argument instead of creating it.
Printable View
What you need to understand is the scope of variables and how to pass through reference or value depending on what you need.
In your example, the function should probably take the vector as argument instead of creating it.
Code:class SomeClass
{
public:
SomeClass() { }
~SomeClass() { }
void Add(int a) { SomeList.push_back(a); }
std::list<int>::iterator GetFirstElement() { return SomeList.begin(); }
private:
std::list<int> SomeList;
}
int main()
{
SomeClass Foo;
Foo.Add(5);
std::list<int>::iterator it = Foo.GetFirstElement();
return 0;
}
Concerning Dark_Phoenix's example - I think that if you want to provide:
You should also provide:Code:std::list<int>::iterator GetFirstElement() { return SomeList.begin(); }
Incidentally, I note that the terminating semi-colon is missing from the class definition.Code:std::list<int>::const_iterator GetFirstElement() const { return SomeList.begin(); }
Hmm, now how did I miss that..? :PQuote:
Incidentally, I note that the terminating semi-colon is missing from the class definition.