简体   繁体   中英

How to look if a pointer in a pointer map is not 0

I made a map, I need that map in another thread, so I made a pointer that points to my map and send it to the other thread. But when I want to look if the value in a map is not NULL (Pointer) I get an error.

This is a example codee:

#include <iostream>
#include <vector>
#include <map>

using namespace std;


int main()
{
    int test = 1;

    map<int,void *> handle;
    map<int,void *> * handle2;

    handle[0] = &test;

    handle2 = &handle;


    if(*handle2[0])
    {
        cout << "Works\n";
    }

    system("Pause");


    return false;
}

This is the error I get:

error C2451: conditional expression of type 'std::map<_Kty,_Ty>' is illegal

How can I cheak for a 0 pointer in this case?

handle2 is a pointer to a map, so handle2[0] (equivalent to *handle2 ) is the map itself. As the error says, this can't be used as a conditional expression.

If you want to check whether handle2 is null, just say handle2 ; for the element of the map with key 0, you want (*handle2)[0] .

if(*handle2[0])

One possibility:

if((*handle2)[0])

another (slightly cryptic for me):

if(handle2[0][0])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM