In dice.h
, I have
class dice {
public:
dice(int sides);
int roll() const;
static int globalRoll(int sides);
private:
int sides;
And in dice.cpp
dice::dice(int sides) {
this->sides = sides;
}
int dice::roll() const {
return rand() % sides + 1;
//Here sides refers to member field
}
int dice::globalRoll(int sides) {
return rand() % sides + 1;
//Here sides refers to parameter
}
Then, for example in a function rollInitiative()
, I have the call
return dice.globalRoll(20) + getDexMod();
Which doesn't work because "type name [dice] is not allowed". I could do the following but I'd rather not create an instance for a single roll call.
dice d(20);
return d.roll() + getDexMod();
My assumption was that I'd be able to call a static function from a class without instantiating it, since my understanding is that static functions don't refer to an instance of the class.