tags:

views:

59

answers:

3

I have a c++ class derived from a base class in a framework.

The derived class doesn't have any data members because I need it to be freely convertible into a base class and back - the framework is responsible for loading and saving the objects and I can't change it. My derived class just has functions for accessing the data.

But there are a couple of places where I need to store some temporary local variables to speed up access to data in the base class.

mydata* MyClass::getData() {
   if ( !m_mydata ) { // set to NULL in the constructor
      m_mydata = some_long_and complex_operation_to_get_the_data_in_the_base()
   }  
   return m_mydata;
}

The problem is if I just access the object by casting the base class pointer returned from the framework to MyClass* the ctor for MyClass is never called and m_mydata is junk.
Is there a way of only initializing the m_mydata pointer once?

+1  A: 

You could initialize your private variables in a separate initialization member function, so something like this:

class MyClass {
  public:
    init() {
      if (!m_mydata) {
        m_mydata = f();
      }
    }
};

framework_class_t *fclass = framework.classfactory.makeclass();
MyClass *myclass = (MyClass*)fclass;
myclass->init();
char *mydata = myclass->getData();

It's hard to say if this is a good idea or not without knowing what framework you're using, or seeing your code. This is just the first thing that came to mind after reading your description.

Ben Collins
That's what I am doing - but it's easy to miss one. I wondered if there was some 'magic' c++ initialization trick I didn't know about to do this automatically.
If you have access to the framework class definition, you could overload the conversion operator, but then that's not good style for frameworks (how many derived types will have to have supported conversions?).If you don't like the manual cast + ::init() method, then perhaps you could use a macro or another function so that you don't forget to call ::init().
Ben Collins
+1  A: 

You could create a wrapper for the factory of the framework. The wrapper would have the same interface, delegate calls to the framework but it could initialize the created base class instance before returning it. Of course, this requires you to change your code to use the wrapper everywhere, but if it is possible, after that you can be sure that the initialization happens properly.

A variation on this: use RAiI by wrapping the base class instances into a custom autopointer which could do the initialization in its constructor. Again, if you manage to change the code everywhere to use the new wrapper type instead of the derived class directly, you are safe.

Péter Török
+1  A: 

It doesn't have members and you must maintain bit-for-bit memory layout compatibility… except it does and C++ doesn't have a concept of freely-convertible.

If the existing framework allocates the base objects, you really can't derive from it. In that case, I can think of two options:

  • Define your own class Cached which links to Base by reference. Make the reference public and/or duplicate Base's interface without inheritance.
  • Use a hash table, unordered_map< Base *, mydata > mydata_cache;. This seems most appropriate to me. Use free functions to look up cache data before delegating to the Base *.
Potatoswatter
Thanks - looks like this was just a dumb thing to do in C++