C ++ 11 auto / I don't understand something

Ok this is my code:

CShop::~CShop()
{
    TPacketGCShop pack;
    pack.header = HEADER_GC_SHOP;
    pack.subheader  = SHOP_SUBHEADER_GC_END;
    pack.size = sizeof(TPacketGCShop);
    Broadcast(&pack, sizeof(pack));
    GuestMapType::iterator it;
    it = m_map_guest.begin();
    while (it != m_map_guest.end())
    {
        LPCHARACTER ch = it->first;
        ch->SetShop(NULL);
        ++it;
    }
    M2_DELETE(m_pGrid);
}

Su, I have GuestMapType::iterator it;this oneit = m_map_guest.begin();

Well, if I make my function like this:

CShop::~CShop()
{
    TPacketGCShop pack;
    pack.header = HEADER_GC_SHOP;
    pack.subheader  = SHOP_SUBHEADER_GC_END;
    pack.size = sizeof(TPacketGCShop);
    Broadcast(&pack, sizeof(pack));

    auto it = m_map_guest.begin();
    while (it != m_map_guest.end())
    {
        LPCHARACTER ch = it->first;
        ch->SetShop(NULL);
        ++it;
    }
    M2_DELETE(m_pGrid);
}

I deleted GuestMapType :: iterator it; simplify my code? My question is. Affect this my program? Any risk?

+4
source share
2 answers

This is great and declares iterators with auto, from my point of view, good practice for at least two reasons:

1- Typically, the type of iterator is quite long. The less you type, the less you make mistakes. It also makes the code clearer because you are hiding implementation details that really don't matter in this context.

2- : , , , , auto. , , .

+5

m_map_guest.begin() GuestMapType::iterator, .

auto - / , , strong, 1. :

int i = 7;
auto i = 7;

7 i.

, auto , C:

ttype *x = malloc(sizeof (ttype));
ttype *x = malloc(sizeof (*x));

, - - - auto, .


1 , , , , , . .

+1

Source: https://habr.com/ru/post/1615010/


All Articles