I know the question is going to sound weird, so let me start of with the issues:
Problem 1
I'm making a Monopoly game (for which a Monopoly
class has been made that has a PlayerList
member, containing players), and I had implemented an Auction
class. I recently discovered that I have a lot of comparable code in the Auction
class and the PlayerList
class, so I put a lot of Auction
-code in the BidderList
class.
// Auction.cpp
Auction::Auction(Property* property, PlayerList* bidderList, int currBidderIndex)
: m_property{ property }, m_bidderList{ new BidderList(bidderList, currBidderIndex) },
m_highestBid{ startBid }, m_previous_has_left{ false }
{
}
// BidderList.cpp
BidderList::BidderList(PlayerList* playerList, int currBidderIndex)
: PlayerList(playerList)
{
m_currPlayerIndex = currBidderIndex;
}
However... in my implementation of an Auction, it's used as a temporary object that will be destroyed after it's done. (Yes, I could give it setters.) Since I'm using the original player objects (which is needed to modify them), I'm also deleting those in the base destructor (unintentionally) when the BidderList
object gets destroyed:
PlayerList::~PlayerList()
{
for (const auto player : m_players)
delete player;
}
... which forced me to do something like this:
// Prevent base class from deleting the players! - not so clean
BidderList::~BidderList()
{
int size = getSize();
for (int i = 0; i < size; ++i)
m_players[i] = nullptr;
}
I still want to use my players after the Auction, since it's not sure the game will have ended. I was wondering if I could do this on a cleaner way.
Problem 2
I was also thinking about making deriving Bidder
and (Trader
for Trade
) from the Player class (which doesn't solve the previous problem in any obvious way), because this way I could again achieve higher cohesion. Although the problem isn't big in this situation, I still would like to know an answer to the following problem:
Somehow, each player acts as a bidder in during an Auction (which is still temporary).
- I can copy each player into a bidder, but then I won't be modifying the actual player (which I want, of course).
- I can make the Bidder in such a way that it contains a pointer to the actual object, that would mean I have to relink every method of the Player to the bidder, where I should actually be using inheritance.
If I somehow manage to convert the actual player to a bidder (so I would be using the original player object), then I still have to make sure only the derived class is destroyed - sounds like since fiction to me. Is there no other way around then using the composite relation here? Because, it would look like this and it ain't pretty:
bool Derived::foo()
{
return m_base.foo();
}
In both problems, I have a need for a temporary derived class, constructed with an existing parent, which needs to be cleaned up afterwards in a safe way. Could you please help me with making the right decision here? (Yes, I do want to keep the Auction object a temporary object, just for practice). Thanks in advance!