Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:32:09+00:00 2026-06-10T18:32:09+00:00

I have a tricky little problem I have run into. I believe the problem

  • 0

I have a tricky little problem I have run into. I believe the problem has to do with the way I am casting things.

So I have a base class called combatEntity. It has the following function

class combatEntity {
public:
    virtual void update();
};

I then have a class mob, which is derived from combatEntity, overrides the update function:

class mob : public combatEntity {
public:
    virtual void update();

}

I then have a class named monster, which is derived from mob and also overrides the update function.

class monster: public mob {
public:
    virtual void update();
}

I have a combatEntity pointer called i:

combatEntity* i;

Then I have:

//returns a mob* pointer (needs explicit cast)
monster* newMonster = getMob();
i = newMonster;

The getMob() function:

mob* getMob() {
   mob* newMob = new mob();
   //set some data in newMob
   return newMob;
}

When I call i->update(), it calls mob::update(), because newMonster is set to “new mob();”, since getMob() returns a new mob pointer. When I call i->update(), I need it to call monster::update(), but using breakpoints, I see it is calling mob::update() and not monster::update().

so I need to create a new monster object, but still have it’s base class data filled with the object returned from getMob(), but have the functions overridden properly. I have also tried dynamic_cast, static_cast and reinterpret_cast, and none seem to work. Or I need to cast my base class to a derived class, while properly overriding functions with the derived class.

Hopefully this makes sence. Any advice would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:32:10+00:00Added an answer on June 10, 2026 at 6:32 pm

    take a look to your hierarchy tree, make sure the classes are related how you think.

    This code

    #include <iostream>
    
    struct combatEntity
    {
        virtual void Update() { std::cout << "Combat Entity\n"; };
    };
    
    struct mob : combatEntity
    {
        virtual void Update() { std::cout << "Mob\n"; };
    };
    
    struct monster : mob
    {
        virtual void Update() { std::cout << "Monster\n"; };
    };
    
    int main(int argc, char **argv)
    {
        combatEntity *ce = new monster;
        ce->Update();
        delete ce;
    
        return 0;
    }
    

    creates this output:

    Monster
    

    Source: http://ideone.com/k6LAB

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tricky problem that has had me scratching my head for a
I have the following tricky problem: I have implemented a (rather complicated) class which
so i have a little tricky combination here Company has many Users User belongs
This is a little tricky so bear with me: I have a PHP script
This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>
I seem to have a tricky problem since the latest ADT update to release
I have a very tricky problem going on with content inside a textarea on
I have sort of a tricky problem I'm attempting to solve. First of all,
I have worked myself into a tricky situation and find myself unable to fight
My problem is a little tricky for me so I will try to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.