0

I'm trying to get a pointer to a function of an instance of my object. Here is my code:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

class Dice {
    int face;
public:
    Dice () {
        face = rand() % 6 + 1;
    }
    int roll() {
        face = rand() % 6 + 1;
        return face;
    }
};

int main()
{
    Dice mydice;
    vector<int> v(1000);
    generate(v.begin(),v.end(),mydice.roll);
}

My compiler barks at me at the generate line with cryptic messages =) Please point out how to properly tell generate to call mydice.roll() to populate vector v.

Ali
  • 56,466
  • 29
  • 168
  • 265
Dima
  • 2,012
  • 2
  • 17
  • 23
  • 6
    `std::bind(&Dice::roll, &mydice);` - or just `[&]{ return mydice.roll(); }`. – Xeo Mar 30 '13 at 02:16
  • possible duplicate of [C++ function pointer (class member) to non-static member function](http://stackoverflow.com/questions/990625/c-function-pointer-class-member-to-non-static-member-function) – Nicol Bolas Mar 30 '13 at 02:17
  • @Xeo please use answers, instead of comments =) – Dima Mar 30 '13 at 02:17
  • @Dima he has way too much rep already – Bartek Banachewicz Mar 30 '13 at 02:18
  • @NicolBolas I have read all of that question, but nobody there provides an answer on how to use std::bind or std::function to achieve what I need. – Dima Mar 30 '13 at 02:18
  • @BartekBanachewicz One can never have enough =) – Dima Mar 30 '13 at 02:18
  • Almost the same: http://stackoverflow.com/questions/4642497/passing-a-member-function-to-for-each (I don't say "duplicate" only because you marked your question specifically for C++11). – jogojapan Mar 30 '13 at 11:13

2 Answers2

3

Give it an object:

generate(..., std::bind(&Dice::roll, &mydice));

std::bind is in <functional> and binds arguments so that a function can be called without supplying them.

chris
  • 60,560
  • 13
  • 143
  • 205
  • Coming from Python & C this syntax is totally non-obvious... Not sure how you all stand it =))) But thanks for the answer! It works \o/ – Dima Mar 30 '13 at 02:25
  • @Dima, It's a functional concept, so you could say that it's non-obvious to a lot of C++ programmers as well. – chris Mar 30 '13 at 02:27
  • Now you mention it, thinking in Lisp-like way, does make it "natural". – Dima Mar 30 '13 at 02:28
1

Another possible approach: make use of the () operator to define your dice class itself as a functional. Include this in your class: int operator()() { return roll(); } , then you can simply call your generator with generate(v.begin(),v.end(), mydice); .

Piotr99
  • 531
  • 3
  • 12
  • I like this answer as it is also valid C++98 which is what my course is based on. – Dima Apr 03 '13 at 11:39
  • 1
    Thanks! Maybe 2 comments from my side: 1) when using the operator(), you need to adjust the class name to keep the code self-explanatory, because you don't automatically include a verb as in the method name "roll". 2) If you want to continue using C++ after the course, do yourself a favor and look into the changes C++11 brings. They're really useful for simplifying many uglinesses of C++03 and make writing readable code much easier. – Piotr99 Apr 03 '13 at 17:02