2 回答
The problem is that getParent()
returns a BaseNode*
, which could be a pointer to any type of BaseNode
- it might point to an unrelated class that also derives from BaseNode
. If you're 100% sure that the parent must be of type Match
, you should cast the parent to a Match*
first, and then you can call getPlayer()
on that:
Player* Set::getPlayer1() const{
return dynamic_cast<Match*>(getParent())->getPlayer1();
}
If the parent isn't necessary a Match
, then dynamic_cast
might return NULL, so be sure to check for that.
I think you really need to re-organize your hierarchy and method names... This has nothing to do with your question, but in general it seems hard to fathom why Set inherits from Match. (doesn't a match have sets?)
games are composed of points, sets are composed of games and a match is composed of sets... a point is won by a player.
you should probably structure it more closely to the real world.
just my $.02
EDIT
I'd probably have a Match object that contains a map of sets (map i.e. set one, two three, etc) and the Method Player(int) rather than Player1() and player2(). Also it does not seem necessary to have a method for player in the Set class. A Set would point to a match in which it is being played.