Static function in abstract class
How do I implement a static function in an abstract class? Where to implement it?
class Item{
public:
//
// Enable (or disable) debug descriptions. When enabled, the String produced
// by descrip() includes the minimum width and maximum weight of the Item.
// Initially, debugging is disabled.
static enableDebug(bool);
};
a source to share
First of all, this function needs a return type. I guess it should be void
.
You implement it in your source file just like any other method:
void Item::enableDebug(bool toggle)
{
// Do whatever
}
There is nothing special about it being static or the class Item
being abstract. The only difference is that you don't have access to the pointer this
(and therefore the member variables) inside the method.
a source to share
Most modern C ++ compilers can (now) handle a static method implementation inside a class declaration, as in:
class Item {
public:
static void enableDebug(bool value) {
static bool isOn = false;
isOn = value;
std::cout << "debug is " << (isOn ? "on" : "off") << std::endl;
}
};
I'm not saying this is a good idea, but some of them are outlined in previous answers.
a source to share