4

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);
}

Soo i have GuestMapType::iterator it; and this it = m_map_guest.begin();

It'f fine 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 removed GuestMapType::iterator it; to simplify my code? My question is. Affect this my program? Any risk ?

Joe.Dc
  • 53
  • 5

2 Answers2

5

That's perfectly fine and declaring iterators with auto is, from my view, a good practice for at least two reasons:

1- Generally, iterator's type are pretty long to type. The less you type, the less you mis-type. It makes the code clearer too, because you hide an implementation detail that doesn't really matter, in that context.

2- Forward compatibility: when you will modify your code, i.e. the name for the iterator type name, you won't have to change the code where you use it with auto. After all, you want to use that type of iterator, independently from its name.

Paolo M
  • 11,431
  • 5
  • 51
  • 69
  • Thank you! C++11 awesome. :)) – Joe.Dc Nov 09 '15 at 08:17
  • Another question. Please. auto can prevent memory leak ? – Joe.Dc Nov 09 '15 at 08:30
  • @Joe.Dc No, it doesn't. If you want to prevent memory leaks, learn about [RAII](http://en.cppreference.com/w/cpp/language/raii) and [smart pointers](http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one). You may also give a look to the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines), a brand new project started by Stroustrup, Sutter and others to change the way C++ is taught and used. – Paolo M Nov 09 '15 at 08:35
1

Provided the return type of m_map_guest.begin() can be determined to be GuestMapType::iterator, there should be no difference.

The auto typing is not some magical dynamic/weak type, it's simply an indicator to the compiler that it should work out the strong type itself, without you being explicit about it1. There is no functional difference between the two lines:

int i = 7;
auto i = 7;

because the compiler uses the type of the 7 in the second example to infer the actual type of i.

You should generally use auto wherever possible for the same reason you should use the second of these in C:

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

The latter requires only one change if the type ever changes to something else - that becomes much more important with auto as the actual type and declaration can be much more widely separated.


1 Of course, where the return type is different, such as implicit conversions or sub-classes, it's a little harder for the compiler to work out what type you want. So it may not work in those cases.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841