Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static function in an abstract class

Tags:

c++

How to implement a static function in an abstract class?? Where do I implement this?

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);

};
like image 441
aherlambang Avatar asked Apr 18 '26 10:04

aherlambang


2 Answers

First of all, that function needs a return type. I'll assume it's supposed to be void.

You implement it in a source file, just as you would implement any other method:

void Item::enableDebug(bool toggle)
{
  // Do whatever
}

There's nothing special about it being static or the Item class being abstract. The only difference is that you do not have access to a this pointer (and consequently also not to member variables) within the method.

like image 174
Tyler McHenry Avatar answered Apr 21 '26 03:04

Tyler McHenry


Static functions cannot be virtual so you implement it in the context of the class itself. It doesn't matter if the class is abstract or not.

void Item::enableDebug(bool)
{    
}
like image 32
Brian R. Bondy Avatar answered Apr 21 '26 03:04

Brian R. Bondy