Generic variable to store varying levels of pointers

I want to write a generic centralized null-checker API for pointer type objects. I want to populate a list of my input pointer objects and call the null-checker API to scan through the list and validate if any pointer object is having NULL value. For this, I need to store varying levels of pointers in a single generic variable. How can I do that in C++ ?

template<typename type>
bool check(type& t)
{
    return false;
}

template<typename type>
bool check(type* t)
{
    return true;
}

int main()
{
    char** doubleCharPointer = NULL;
    char* charPointer = "charPointer";
    doubleCharPointer = &charPointer;
    char** temp = doubleCharPointer;
    char* temp1 = NULL;

    int count = 0;

    if(!check(temp))
    {
        cout << "nNot a pointer" << endl;
        return 0;
    }

    temp1 = *temp;
    count++;

    if(!check(temp1))
    {
        cout << "nPointer level : " << count << endl;
        return 0;
    }

    count++;

    cout << "nPointer level : " << count << endl;
}

This is my null-checker prototype. It is very static at the moment. But I wanted to extend it to support any level of pointer checking. For that I need “temp” to be able to hold any level of pointer so that I can run an infinite while loop till all the levels of the input pointer is consumed and validated. How can I do that ? Please help.


Source: c++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.