c++ type erasure

Jun 14, 2018 at 2:45pm
Hello I have problem in my school work
I try to pass in the "Board board" and the Color color to the the Move PlayeMove(const Board& board, Colour c) function, I declare inside struct Model:Concept and I not sure am I declare it correctly?


The following question is

1. Did I declare the 2 variable in correct position

2. How do I pass the 2 variable into player move function? should I pass it in
Model(U&& t)?


I appreciate give me some hint and maybe some tutorial I can follow, prefer with function that need to pass in parameter to call. I been searching online, most of the tutorial is calling const function that did not have any parameter.

https://www.youtube.com/watch?v=ZPk8HuyrKXU
I also found that it will be better if using unique pointer for concept ptr, so far I did not learned this yet.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Player
{
  public:
  // forwarding constructor
  template<typename T,
         typename U = typename std::enable_if
         <!std::is_same<typename std::decay<T>::type,Player>::value>::type >
  Player(T&& t)
  : ptr(new Model<T>(std::forward<T>(t)))
  {  
    
  }
  // copy constructor using clone 
  Player(const Player& rhs)
  : ptr(rhs.ptr->clone())  
  {
      
  }
  // copy assignment
  Player& operator=(const Player& rhs)
  {
      Concept *tmp(rhs.ptr->clone());
      delete ptr;
      ptr=tmp;
      return *this;
  }
  // destructor
  ~Player()
  {
     delete ptr;
  }
  
  //  the function that the human player and computer player have
  Move PlayeMove(const Board& board, Colour c)
  {
      
  }
    
  private:
    // the concept struct
    struct Concept
    {
      virtual Concept* clone() = 0;
      virtual Move PlayeMove(const Board& board, Colour c) =0;
      ~Concept()
      {
        
      }
    }
    
    // the model struct inherit form the concept
    template<typename T>  
    struct Model : Concept
    {
      template<typename U>
      Model(U&& t)
      :obj(std::forward<U>(t))
      {
        
      }
      
      Model<T>* clone() 
      { 
        return new Model(obj);
      }
      
      Move PlayeMove(const Board& board, Colour c)override;
      { 
        obj.PlayeMove(board, c); 
      }
      T obj;

     // the variable i want pass it in to move
      Board board;
      Colour colour;
      /****************************************/
    }
    Concept *ptr;
};

class HumanPlayer
{
public:
Move PlayMove ( const Board& board, Colour c )
{

}
};

class ComputerPlayer
{
public:
Move PlayMove ( const Board& board, Colour c )
{

}
};
Last edited on Jun 14, 2018 at 2:47pm
Jun 15, 2018 at 1:59am
Why must ComputerPlayer and HumanPlayer be unrelated classes?
Jun 15, 2018 at 2:42am
Because, human player can choose the move and computer player will need implement ai logic
Jun 15, 2018 at 4:58am
Typically, the solution would be to introduce straightforward run-time polymorphism: both player classes would inherit from a single Player class, and implement a virtual function named Move.

Type erasure is a mechanism to unify static and run-time polymorphism. It allows you to homogenize types that each supply a similar interface, when those types are entirely unrelated - i.e., when they don't (or can't be made to) share a base class supplying that interface. It isn't a technique that you would ordinarily apply.

You'd pass the arguments to Player::PlayeMove when you call it.
Last edited on Jun 15, 2018 at 5:01am
Topic archived. No new replies allowed.