![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 2
| ![]() Anyways, I'm currently writing a game in XNA. As part of it I've got a base class for all of the movable objects in my game and have derived a player class which is used to create all of the object classes for players. As part of that I've added a Update class with the intention of calling the other Update class in the base class. This currently looks like this; Code: ActorBase class - protected virtual void Update()
{
UpdateStuff
}
Player class - public new void Update()
{
base.Update()
PlayerSpecificUpdateStuff
}
Code: ActorBase[] obj; obj = new Player[15]; obj[0] = new Player(ConstructorStuff); obj[0].Update() Thanks in advance for any help! |
| CaptainMaxxPow is offline | |
| | #2 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 462
| According to what you posted your ActorBase Update() method is protected, meaning that only classes that derive from it will be able to call this function. Instead you should make this public. Polymorphism won't happen until runtime, therefore to the compiler it just see's you trying to call a protected method and lets you know politely by not compiling. Yes you can use a foreach in this manner becasue afterall ActorBase "Is-A" Player. Last edited by valaris; 04-23-2009 at 08:30 AM. |
| valaris is offline | |
| | #3 |
| Registered User Join Date: Apr 2009
Posts: 2
| I like the idea of the compiler being polite. I guess an impolite compiler would say "Hey idiot, you can't call protected members. What are you, stupid?" :P Making it public makes sense since I never really use the BaseClass in the construction of any objects in-game, so it wouldn't matter too much if I were to make it public. Now though it seems that when I create the object in the same way as I did before, the program goes directly to the ActorBase's Update function, rather than going to the Player Update function. Do I have to specify certain function attributes in order for the compiler to use the Player Update rather than the ActorBase one? EDIT: With a bit of testing, I've confirmed that the compiler, though I've specifically called the class and constructor for a Player object, is still considering obj[0] as an ActorBase class. When I add an empty function to Player the program doesn't recognise it as a valid function as part of obj[0], even though obj[0] in theory should be a Player object. Last edited by CaptainMaxxPow; 04-23-2009 at 08:52 AM. |
| CaptainMaxxPow is offline | |
![]() |
| Tags |
| class, inheritance, polymorphism |
| Thread Tools | |
| Display Modes | |
|