-1

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.

  • 2
    `dice.` -> `dice::` – Bathsheba Dec 01 '21 at 18:03
  • 1
    Your guesswork is almost good:`return dice::globalRoll(20) + getDexMod();` – πάντα ῥεῖ Dec 01 '21 at 18:04
  • _"I'd rather not create an instance for a single roll call."_ Why? Do you dislike how the code looks? Do you think it would be slower? Do you know it can still be a one-liner? `dice(20).roll()` – Drew Dormann Dec 01 '21 at 18:04
  • @DrewDormann Although I'm not too familiar with memory management, I feel like it's ineffective to instantiate something that doesn't need to be. I'd also assume it's negligibly slower. but when we get down to it I just don't like having two lines when I feel like I could do it in one. – Justin Iaconis Dec 01 '21 at 18:08
  • 2
    So, once you have this working, you have two **different** ways to generate the roll of a die: you can create an object that has the number of sides and call its `roll` member function, or you can call the `roll(int)` function and pass it the number of sides. You should probably pick one or the other. Having two ways of doing the same thing leads to confusion, especially if you decide you need to change how you do it, and you have to make the same change in two different places. – Pete Becker Dec 01 '21 at 18:11
  • @JustinIaconis I think your assumptions are wrong. Both "slower" and "two lines". See my earlier comment. Initializing a `dice` requires initializing an `int`, which you are doing in all cases. – Drew Dormann Dec 01 '21 at 18:12
  • @DrewDormann so the cost of initializing a class is just the sum of initializing all of it's fields? – Justin Iaconis Dec 01 '21 at 18:20
  • @JustinIaconis If you're really at the stage where you need to scrutinize assembly instructions, [let's look at the assembly instructions](https://godbolt.org/z/8Y55E1TEj). – Drew Dormann Dec 01 '21 at 18:29
  • @DrewDormann I'm sorry I'm not trying to scrutinize I'd just like to know as much as possible. And that's a wicked website, thank you – Justin Iaconis Dec 01 '21 at 18:40

1 Answers1

0

Ok I just needed to change dice. to dice::. I feel silly. I don't really understand the difference but I'll look into it.

Also, since overhead is irrelevant and it's bad practice to have different paths to accomplish the same thing (and most importantly it can still be all on one line), I have just deleted the static function and will instead use

dice(int).roll();