0

For a class, which is only defined in a header, I need a special behavior of one method for all instance of the class. It should be depending on a default value, which can be changed any time during runtime. As I do not want a factory class nor a central management class I came up with that idea:

class MyClass
{
public:
    void DoAnything() // Methode which should be act depending on default set.
    {
        // Do some stuff
        if(getDefaultBehaviour())
        {
            // Do it this way...
        }
        else
        {
            // Do it that way...        
        }
    }

    static bool getDefaultBehaviour(bool bSetIt=false,bool bDefaultValue=false)
    {
        static bool bDefault=false;
        if(bSetIt)
            bDefault=bDefaultValue;
        return bDefault;
    }
};

It works, but it looks a little awkward. I wonder if there is a better way following the same intention. In the case where I want to use it the software already created instances of that class during startup and delivered them to different parts of the code. Eventually the program gets the information how to treat the instances (for e.g. how or where to make themselves persistent). This decision should not only affect new created instances, it should affect the instances already created.

dyp
  • 38,334
  • 13
  • 112
  • 177
Martin Schlott
  • 4,369
  • 3
  • 25
  • 49

2 Answers2

2

I'd advise to use a simple method to simulate a static data member, so the usage becomes more natural:

class MyClass
{
public:
    // get a reference (!) to a static variable
    static bool& DefaultBehaviour()
    {
        static bool b = false;
        return b;
    }

    void DoAnything() // Methode which should be act depending on default set.
    {
        // Do some stuff
        if(DefaultBehaviour())
        {
            // Do it this way...
        }
        else
        {
            // Do it that way...        
        }
    }
};

where the user can change the default at any time with

MyClass::DefaultBehaviour() = true;
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • Yeah, that looks much better. The only trade off is that it is not so good to understand for c++ beginners as it manipulates a return value. Anyway, it is better than my solution. – Martin Schlott Jan 07 '14 at 17:57
0

My thanks to Daniel Frey with his answer which I already marked as the best. I wanted to add my final solution which is based on the answer from Frey. The class is used by some c++ beginners. As I told them to use always getter and setter methods, the way described by Frey looks very complex to beginners ("uuuh, I can give a function a value?!?!"). So I wrote the class like followed:

class MyClass
{
public:
    // get a reference (!) to a static variable
    static bool& getDefaultBehaviour()
    {
        static bool b = false;
        return b;
    }
    static void setDefaultBehaviour(bool value)
    {
        getDefaultBehaviour()=value;
    }

    void DoAnything() // Methode which should be act depending on default set.
    {
        // Do some stuff
        if(getDefaultBehaviour())
        {
            // Do it this way...
        }
        else
        {
            // Do it that way...        
        }
    }
};

for the user, I looks now like a usual getter and setter.

Martin Schlott
  • 4,369
  • 3
  • 25
  • 49