Follow the White Rabbit

One of the things that constantly, and consistently, breaks in Dredmor is our save game system. I’m looking into new ways of fixing this for future projects while I keep the old one up and running. If I get a winner, well, I may retrofit Dredmor with it. If I don’t, well, who knows.

As part of this research, I stumbled across a C++ feature recently — I use the term “feature” loosely, as with all C++ features — that seems like it might be useful. At the very least, it’s not something that I knew you could do in C++ before, and after working with C++ for the better part of 15 years, very little about the language surprises me. So, follow the white rabbit:

class X 
{
  public: 
  int memberIntA;
  int memberIntB;

};

int *X::myPointer = &X::memberIntA; 
X a; 
int n = a.*myPointer;

myPointer = &X::memberIntB;
int m = a.*myPointer;

What is going on here?!

Well, it turns out that you can have pointers to class data members in much the same way that you can have pointers to member functions. I was familiar with the joys of pointer to member functions, which are very useful for having callbacks. I was not, however, familiar with the idea that you could have pointers to member objects. Apparently, nobody has ever really done anything with this either. So there you go! A new, useful, piece of C++. I wonder what else the language has to offer?

Posted in Programming | 2 Comments

2 Responses to “Follow the White Rabbit”

  1. Tuxedo says:

    >I wonder what else the language has to offer?

    operator,() overloading 😉 (no, that’s not a joke)

    { reply }
  2. Nicholas says:

    Tuxedo:

    Yeah, I knew about that one. I think I most recently saw it after deciding to re-read Alexandrescu’s “Modern C++ Design” where he uses it as part of his Command pattern implementation of Functors.

    As I said, it’s not that I don’t *know* the evils of C++. Dredmor doesn’t do much of them, but the actual Gaslamp engine codebase (which we aren’t using for Dredmor, but for the next thing) uses a lot of templated code to great effect. Most notably, Dredmor uses a simple factory on a single hierarchy to implement its load/save system, whereas in the Gaslamp engine we do everything with templates and a homegrown reflection library.

    { reply }

Leave a Reply

Your email address will not be published. Required fields are marked *