[gdal-dev] Refactoring: class members clean-up

Even Rouault even.rouault at spatialys.com
Sat Oct 17 03:54:46 PDT 2015


Le samedi 17 octobre 2015 00:25:45, Kurt Schwehr a écrit :
> I personally like foo_, but m_ is fine.  I worry about unexpected side
> effects of changing member naming with private members.  I don't think
> there will be any if everything builds, but I still worry.

There can be subtle effects indeed, so care should be taken.

Consider the following example (just for the fun) :

#include <stdio.h>
#include <strings.h>  /* const char *index(const char *s, int c); */

class foo
{
        typedef const char* (*ptrfnc)(const char*, int);
        ptrfnc index;

    public:

        foo() : index(0) {}

        ptrfnc getIndex()
        {
            return index;
        }
};

int main(int argc, char** argv)
{
    printf("%p\n", foo().getIndex());
    return 0;
}

==> Displays (nil)

Now let's rename index to m_index, but forget to change getIndex():

#include <stdio.h>
#include <strings.h>  /* const char *index(const char *s, int c); */

class foo
{
        typedef const char* (*ptrfnc)(const char*, int);
        ptrfnc m_index;

    public:

        foo() : m_index(0) {}

        ptrfnc getIndex()
        {
            return index;
        }
};

int main(int argc, char** argv)
{
    printf("%p\n", foo().getIndex());
    return 0;
}

This still compiles without warning but displays 0x400548 ...

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list