I come from Java side where if you want to check if a particular entry in an ArrayList is allocated or not, you get the reference of the entity in ArrayList and check to see if it is null or not.
Basically I am trying to accomplish the same thing. Here is a simplified version of my code:
struct node
{
int count=0;
int nodeval=0;
bool valid=false;
};
bool operator==(const node* n,int val)
{
if (n->count==0&&n->nodeval==0&&n->valid==false)
if(val==NULL)
return true;
else
return false;
}
node* lookup(int val)
{
return &Table[val];
}
int main()
{
res=somevalue;
node* child=lookup(res);
if(child==NULL)
{
//do something
}
}
Here is my understanding:
lookup()is going to return me the address of a select position in Table data structure. I store that in child.- Checking child for null pointer didn't make sense because we return a proper reference of a memory location so I overload it.
- In the overload
==, I check if each of the fields is unaltered, if it is so it is not changed and hence a 'null entity'. The problem is that==operator requires one user-defined object to operate upon.
Questions:
- What did I get wrong in fore-mentioned steps?
- Is there an easier way to check if an object pointed to by a pointer isn't altered/null?
If you really want to do this the Java wayAnd thus the reason why I stated to the OP that C++ is not Java. Emulating Java in C++ leads to code that is either hard to understand, or just plain wrong / weird.