Is it to return the address of an element in a vector
-
std::vector<myclass> vec;
//...
MyClass & get_selected()
{
std::list<myclass>::iterator it=vec.begin();
for (;it!=vec.end();++it) {
if (it->is_selected())
return &*it; // Is it the address of the element in the vector,
// and is it safe?
}return NULL;
}
-
std::vector<myclass> vec;
//...
MyClass & get_selected()
{
std::list<myclass>::iterator it=vec.begin();
for (;it!=vec.end();++it) {
if (it->is_selected())
return &*it; // Is it the address of the element in the vector,
// and is it safe?
}return NULL;
}
followait wrote:
Is it the address of the element in the vector, and is it safe?
Yes,
&*it
is the address of the vector element that the iterator refers to. Whether it's "safe" or not depends on what you mean by "safe". It is safe regarding if the address returned really is the address of the contained element. But considering that you are handing out a non-const pointer or reference to the object, the caller may change it at will. This could of course be considered as "not safe" in some way. However, your code snippet doesn't seem to compile correctly. In your function declaration you say that the return type is a reference to amyclass
object, but you actually return a pointer to it unless you've omitted something important. The return statement should read 'return *it;
'."It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
std::vector<myclass> vec;
//...
MyClass & get_selected()
{
std::list<myclass>::iterator it=vec.begin();
for (;it!=vec.end();++it) {
if (it->is_selected())
return &*it; // Is it the address of the element in the vector,
// and is it safe?
}return NULL;
}
Design suggestion: you're probably better off following what the STL algorithms do in this case, and return an iterator:
std::vector<myclass> vec; //... std::vector<myclass>::iterator get_selected() { for (std::vector<myclass>::iterator it=vec.begin();it!=vec.end();++it) { if (it->is_selected()) return it; } return vec.end(); }
or even use the algorithms provided in the STL...
std::vector<myclass> vec; //... std::vector<myclass>::iterator get_selected() { return std::find_if(vec.begin(), vec.end(), std::mem_fun_ref(&myclass::is_selected)); }
-
Design suggestion: you're probably better off following what the STL algorithms do in this case, and return an iterator:
std::vector<myclass> vec; //... std::vector<myclass>::iterator get_selected() { for (std::vector<myclass>::iterator it=vec.begin();it!=vec.end();++it) { if (it->is_selected()) return it; } return vec.end(); }
or even use the algorithms provided in the STL...
std::vector<myclass> vec; //... std::vector<myclass>::iterator get_selected() { return std::find_if(vec.begin(), vec.end(), std::mem_fun_ref(&myclass::is_selected)); }
-
std::vector<myclass> vec;
//...
MyClass & get_selected()
{
std::list<myclass>::iterator it=vec.begin();
for (;it!=vec.end();++it) {
if (it->is_selected())
return &*it; // Is it the address of the element in the vector,
// and is it safe?
}return NULL;
}
I think you can't save this address, for example as a class member. If you add new elements to the vector it may need additional memory, which means he must allocate new memory for the whole vector and copy all his data. So every address to the old memory will be invalid afterwards.
-
std::vector<myclass> vec;
//...
MyClass & get_selected()
{
std::list<myclass>::iterator it=vec.begin();
for (;it!=vec.end();++it) {
if (it->is_selected())
return &*it; // Is it the address of the element in the vector,
// and is it safe?
}return NULL;
}
Even if you change to returning an iterator, be careful about doing any operations that invalidate the iterator.
--Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen