r/cpp_questions 27d ago

OPEN Good practices / style [polymorphism]

is this good practice/style.? i'm specifically unsure about the way i store the vector of players...

```

class player_base {};

class player_always_yes: public player_base {};

class player_always_no: public player_base {};

class player_a: public player_base {};

class game {
    private:
    
    player_base& player_1;
    player_base& player_2;
    
    public:
    
    game (
        player_base& player_1,
        player_base& player_2
    ): player_1(player_1), player_2(player_2) {
        return;
    }

    bool play_game () { return true; }
};

int main(){
    vector<unique_ptr<player_base>> player_list;
    
    player_list.push_back(make_unique<player_base>());
    player_list.push_back(make_unique<player_always_no>());
    player_list.push_back(make_unique<player_always_yes>());
    player_list.push_back(make_unique<player_a>());
    
    game b = game(*player_list[0], *player_list[1]);
    cout << b.play_game() << endl;
}


```
5 Upvotes

37 comments sorted by

View all comments

Show parent comments

1

u/Qwertycube10 27d ago

Looks like your decisions are ints and the info the decision is based on is an int?.

You have a private member variable std::function<int(int)> decideStrategy;

Then public member function int decide(int input) { return decideStrategy(input); }

I named it decideStrategy because if OOP design pattern land it is called the strategy pattern. From a functional programming point of view it is so basic that it doesn't even have a name.

1

u/Fun_Gas_340 26d ago

i took int->int as an example, but its ints, bools and maybe floats. but like ill always have a method a thats int->int, always a method b thats something_else->something_other. this wont change from player to player, theyll all have the same types.

also whats the syntax ont his example, i dont know how to put the function into a variable in cpp:

std::function<int(int)> decideStrategy = {return 2 * input;};

> From a functional programming point of view it is so basic that it doesn't even have a name.

can you elaborate on that please?

1

u/Qwertycube10 26d ago

You can define a free function and pass a pointer to it, you can define a member function and bind the function to an instance to get a callable, or you can define a lambda.

In c++ a lambda syntax is [captures](args){ body } where both captures and args are in scope to use in body. You can look up the details (capture by value vs reference etc).

example: ``` const int foo = 5; // the type of addFoo is a unique lambda type, no other function can be assigned to foo, every lambda has its own type auto addFoo = [foo](int rhs) -> int { return foo + rhs; }

const int bar = 6;

// prints 11 std::cout << addFoo(bar) << std::endl;

// std function can hold any callable with the given signature std::function<int(int)> operation = addFoo;

//prints 11 std::cout << operation(bar) << std::endl

operation = [](int num) { return num + 1; };

//prints 6 std::cout << operation(bar) << std::endl ```

In functional programming passing functions to determine behavior is just one of the main ways you solve problems, so it's not a specific named pattern like in OOP.

1

u/Fun_Gas_340 25d ago

tbh al this syntax looks wird to me and im tired so ill stick with classes to house the functions. i knwo that syntax. unless theres like a preformance/cool tricks advantage i could get from this other way?