0
Subclass interact with other subclass?
Lets say I have a superclass RoomObjects. class Player and class Door are subclasses. How is a good way for these two subclasses to interact with each other? Only way I can think of is storing them in a vector of Objects and check them in a third class, Room. Is there no way for a Player object to have a method that takes a Door object as parameter? Like player.interactDoor(&door). That would prevent iteration over a vector. Or is it better to just scratch this and have Player and Door not as subclasses?
2 odpowiedzi
+ 1
Your class of room objects should only contain attributes that are common to other room objects. one of those attributes should probably be the room they belong to. Then you can have the player do something like, room.findDoor(doorName).walkThrough()
The class for the room should have the list of objects in the room like doors, windows, bed, table, etc... The find door method should iterate through that list to find a door with that identification value.
Hope that makes sense.
0
I will try to make it like that. Thank you!