variable global const "macros" in C++ and optimal design patterns

130 views Asked by At

I inherited some 10 year old code I have to complete. The code is in MFC (C++).

There's a .h file where the custom data structures are written and some const variables are in there as Globals. Some of these are used for MS Office file extensions, of type CString, and are declared as _T(".doc"), _T(".xls"), etc.

Obviously these are dated and need to be updated to recognize the Office 2007 and later extensions. My first brilliant idea was to use const_cast to change the constant if needed, but found out later that's a no-no and resulted in undefined behavior (sometimes it would switch back to .doc).

I then decided to create a struct and have two structs inherit from it. I created a void method in the base struct to make it abstract but otherwise it does nothing. Here's the code:

struct eOfficeExtensions{

    const CString WORD_EXTENSION;
    const CString EXCEL_EXTENSION;
    const CString WORDPAD_EXTENSION;
    const INT EXTENSION2007;

    eOfficeExtensions(CString word, CString excel, CString wordpad, INT ver) : 
        WORD_EXTENSION(word), EXCEL_EXTENSION(excel), WORDPAD_EXTENSION(wordpad), EXTENSION2007(ver){}

    //method to ensure base class is abstract
    virtual void Interface() = 0;
};

struct eOfficeExtensions2003 : public eOfficeExtensions{

public:


    eOfficeExtensions2003() : eOfficeExtensions(_T(".doc"), _T(".xls"), _T(".rtf"), 0){}

private:
    virtual void Interface(){}
};

struct eOfficeExtensions2007OrLater : public eOfficeExtensions{


    eOfficeExtensions2007OrLater() : eOfficeExtensions(_T(".docx"), _T(".xlsx"), _T(".rtf"), 1){}

private:
    virtual void Interface(){}
};

This feels like a ridiculous amount of code for what should be a simple conditional definition. What would an experienced programmer do?

EDIT

These constants should only be set once and never changed. The version of MS Office installed is determined by scanning registry subkeys in a class that deals with memory management.

The constants are mainly used to create new files or search a directory for files with that extension, not for resolving conditional statements. The struct should also be instantiated once as a eOfficeExtensions* pointer to the relevant child struct.

1

There are 1 answers

1
molbdnilo On BEST ANSWER

Your inheritance tree essentially defines two different values for the base struct.
You don't need inheritance just to define those values, you only need two variables:

struct eOfficeExtensions{
    const CString WORD_EXTENSION;
    const CString EXCEL_EXTENSION;
    const CString WORDPAD_EXTENSION;
    const INT EXTENSION2007;
};

const eOfficeExtensions extensions2003{_T(".doc"), _T(".xls"), _T(".rtf"), 0};
const eOfficeExtensions extensions2007{_T(".docx"), _T(".xlsx"), _T(".rtf"), 1};

const eOfficeExtensions* extensions = 0;

// ... Later ...

if (office2007Installed)
    extensions = &extensions2007;
else
    extensions = &extensions2003;