Modifying (adding to) an inherited function.

Hi all, I think this should be a fairly simple problem, but I have been staring at my screen for half an hour trying to figure out what's wrong and nothing is coming to me.

So I have an entity manager (it's std::map, if it matters) that calls Update() on all of the entities I've added into it.

I'm writing a subclass of Entities called GlobEntity, and I'd like to add on to the default Update() function. In other words, call Entities::Update() and then do additional work. For some reason, when my entity manager Update()'s, it is reverting to the base class's Update(). I checked to make sure my return type, parameters, etc. were all matching, so I just don't know quite what's wrong. Here are small snippets:

From entitymanager.cpp:
1
2
3
4
5
6
7
8
9
void EntityManager::UpdateAll(float gravAcceleration, sf::Vector2f center)
{
    std::map<std::string, Entities *>::const_iterator itr = _entityMap.begin();
    while (itr != _entityMap.end())
    {
        itr->second->Update(gravAcceleration, center);
        itr++;
    }
}


From globentity.cpp:
1
2
3
4
5
6
7
8
9
10
int GlobEntity::Update(float gravAcceleration, sf::Vector2f centerOfMap)
{
    Entities::Update(gravAcceleration, centerOfMap);
    std::cout << "got here" << std::endl;

    //stuff I want to add.

    return 0;

}


and here's the base class's Update() function:
1
2
3
4
5
6
7
int Entities::Update(float gravAcceleration, sf::Vector2f centerOfMap)
{
   //doesn't really matter what's in here, just showing you the parameters,
   //return type, and function name are the same.

    return 0;
}


The "got here" in GlobEntity is not showing up. Any thoughts?
Is the Entities' Update function virtual?
No... should it be?
Possibly, depending on how your objects are implemented.

Consider this:
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
#include <iostream>

using namespace std;

class Super
{
public:
   void Print()
   {
      cout << "Super" << endl;
   }
};

class Sub:public Super
{
public:
   void Print()
   {
      Super::Print();
      cout << "Sub" << endl;
   }
};

int main(int argc, char* argv[])
{
   Super *my_obj;
   my_obj = new Sub();

   my_obj->Print();
   return 0;
}


You'll notice that even though it's a Sub object, it'll use the Super's function because it's not virtual.

Try changing the Super's print declaration to:
 
virtual void Print()


If you run it now, you'll see that the Sub class's Print function is used and, in doing so, the Super class function is also called.

Hope this is of some help.
I have nothing understood . Could you use a human language?! What shall "got here" show?!
Last edited on
@iHutch. Awesome, I will give that a shot. That would make sense, of course.
Edit: Yup! That did it. I knew it would be a simple answer. Thank you so much! I still have much to learn it seems...

@vlad. Lol. If it's a serious question, it's just my way of knowing that that specific Update() function had been called. Doesn't mean anything.
Last edited on
No problem. Glad I could help. :-)
Topic archived. No new replies allowed.