1

I'm pretty new to c++ but I'm trying to port over a flash game I've started. I'm stuck trying to pass a function in as a parameter, and then set a property equal to that function.

class Param
{
    private:
    float function();
    float value;

    public:
    Param(float (func)());
    Param(float (func)(), float value);
    ~Param();

    void update();
};

I've tried something like:

Param::Param(float (func)()) {
    this.function = func;
}

But it doesn't seem to like that. I'm not sure if it needs to be a pointer either, I'd like to just specify it when the Param is instantiated rather than pass a reference that might get deleted to it.

EDIT: If someone could also answer, is there a way to make these passed in functions optional? As in a function for it to default to if none is specified?

Evan Ward
  • 1,371
  • 2
  • 11
  • 23
  • `this` in C++ is a pointer, and pointers use a special syntax for dereferencing. E.g. `this->function` instead of `this.function` (the latter is illegal). Also, `this->something` inside a (non-template) class is typically redundant, just `something` is sufficient. – dyp Jun 30 '14 at 14:18
  • 1
    `float function();` declares a member function, not a "property" (which is called a *data member* in C++ speak) Functions are no objects in C++, hence, they cannot be changed. – dyp Jun 30 '14 at 14:19

1 Answers1

5

Normally, you´ll need a pointer

class Param
{
    private:
    float (*function)();
    float value;

    public:
    Param(float (*func)());
    Param(float (*func)(), float value);
    ~Param();

    void update();
};

The & for the parameter in an actual call is not needed,
you can just pass the pure name of a function
(with or without &, but without any parenthesis!)

But: The passed function can´t be part of some class instance.
Only "raw" functions (maybe in a namepace) or static class methods.
Use a std::function to enable class instance methods and lambdas etc. too:

class Param
{
    private:
    std::function<float()> function;
    float value;
    public:
    Param(std::function<float()> func);
    Param(std::function<float()> func, float value);
    ~Param();

    void update();
};

A normal function like above can be passed anyways.
(Take a look at std::bind & Co to get an idea how to pass class methods)

deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • Is there a reason `using namespace std;` doesn't stop the `std::` in `std::function...` from being necessary? – Evan Ward Jun 30 '14 at 14:21
  • If you have a `using namespace std;` then yes, you can omit the `std::`. But I don´t know if you have, and it´s bad style anyways (problematic if the names contained in multiple namespaces overlap) – deviantfan Jun 30 '14 at 14:24
  • So is the best practice to just not use any namespaces? – Evan Ward Jun 30 '14 at 14:25
  • Of course you can use it, but write the full name out instead of a `using namespace`. If you have multiple different namespaces, this "using" will make problems (the probably most important reason for namespace is that you can use names which are already used in std...) – deviantfan Jun 30 '14 at 14:27
  • Namespaces are great, but `using namespace x;` subverts them. Please read the first two answers to this question: http://stackoverflow.com/q/1452721/10077 – Fred Larson Jun 30 '14 at 14:37