So I am facing a weird issue. I have a tree of A's & am trying to find the min of a specific field within all of the A's. I do not have a vector of ptrs of ALL the nodes within the manager class. I merely have the root nodes stores there. Each node will however have a vector of ptrs of all it's children though.
Now if I call into this using the following:
std::vector<A*> treeRoots; //only keeps the roots of each tree
A* minVal = nullptr;
time_t minTime = std::numeric_limits<time_t>::max();
for(auto root : treeRoots){
root->findMin(minVal, &minTime);
}
This calls into the following:
void A::findMin(A* minA, time_t* pMinVal){
//find the smallest amongst the children
for(auto child : children_){ //children_ is an std::vector<A*>
child->findMin(minFactor, pMinVal);
}
time_t currentNodeData = getValue(); //the value we are comparing
if(currentNodeData < *pMinVal){
minA = this;
*pMinVal = currentNodeData;
}
log()->info("is null? %d",(minA == nullptr));
}
I seem to always get a true on the "is null?" comparison.
If I modify as follows, it seems to work fine:
for(auto root : treeRoots){
root->findMin(&minVal, &minTime);
}
And call into the following function:
void A::findMin(A** minA, time_t* pMinVal){
//find the smallest amongst the children
for(auto child : children_){ //children_ is an std::vector<A*> within each node
child->findMin(minFactor, pMinVal);
}
time_t currentNodeData = getValue(); //the value we are comparing
if(currentNodeData < *pMinVal){
*minA = this;
*pMinVal = currentNodeData;
}
log()->info("is null? %d",(*minA == nullptr));
}
Since you are passing the value of pointer minVal , which was initally zero, it will always return true because original value of minVal will not be modified. if you want to modify the value, use reference to a pointer like:
root->findMin(minval,&time);
then
void findMin(A* &minA,time_t *pminVal)
{
.....
}
or a pointer to a pointer as you have used in second example.
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.