Protocol Buffers and multiple inheritance

By | November 1, 2013

After I posted Google Protocol Buffers Where Have You Been All My Life, I started thinking about the multiple inheritance issue a bit more.  After a couple of initial trials, I believed it wasn’t going to work.  I kept thinking about that, became somewhat obsessed and needed to figure out why it wasn’t going to work.

So today I started in again, and with a little bit of googling discovered that it can work.  I’ve never used multiple inheritance before in any of my code, as there wasn’t a real need to.  This particular bit of code I’m writing now, though, would benefit greatly from it.

As I said in my previous post, I’ve got a DatabaseRecord class.  It contains functions that are common to all database accesses, including memcache storage and retrieval:


I then have the protocol buffer class Customer, based on this message structure:

From there, I can now create my CustomerRecord class:

The little bit that I was missing before was the public declarations on both of the classes it inherits from.  Everything works as I had hoped it would before.

Now, when I want to extract data from a customer record, I can write code like so:

All records are cached in memcache and if its not found will automatically save the protocol buffer into memcache after it retrieves it from the database.

Its extremely fast, easy to read and easy to maintain.

Leave a Reply

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