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 6352875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:18:36+00:00 2026-05-24T22:18:36+00:00

Alright so I’ve been working on a game engine, and I’m running into a

  • 0

Alright so I’ve been working on a game engine, and I’m running into a problem… Currently I have a hierarchy of classes (image->animation->object) all of which have a method get_type() and I’m having this problem:

if I declare a derived class using the new keyword, or statically, I get the desired results:

object instance;
cout << instance.get_type(); //returns an int value  

object* pointer = new(object);
cout << pointer->get_type();

From the above code, the console outputs 3. The method is declared as such in object:

Image Class:

class image{
public:
  virtual int get_type();
};

int object::get_type(){
  return 1;
}

Animation Class:

class animation: public image{
public:
  virtual int get_type();
};

int animation::get_type(){
  return 2;
}

Object Class:

class object: public animation{
public:
  virtual int get_type();
};

int object::get_type(){
  return 3;
}

Now the problem arises when I do something like this:

object* t = &object();
cout << t->get_type();  

The result is now 1.

If I remove the virtual keyword from the class declaration of the object class, it works as I expect (the last bit of code returns 3)

Question: how can I utilize my virtual methods from the object* pointer without using the new keyword? Reason being, I’m filling a vector with pointers to these objects and creating them as I’m adding them, so I hope to be able to do something like the following:

vector<object*> objects; //not imgs or animations
for (int a = 0; a < 20; a++){
  objects.push_back(&object());
}

And have any object[x]->get_type(); return the correct class type. The reason for this is an event system that I’m using in my game that needs the origination class type…

TLDR; I really stink at inheritance, can you help?

  • 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-05-24T22:18:36+00:00Added an answer on May 24, 2026 at 10:18 pm

    This is because both the constructor and destructor have run in your code:

    object* t = &object();
    cout << t->get_type(); 
    

    The &object() creates an (anonymous, temporary) object, immediately destructs it (because it’s temporary), and then assigns the address of the now-destructed object to t. (Your compiler may warn you about this expression if you turn the warning level up high enough.)

    Try creating an object that sticks around during the time you’re using it:

    object my_object();
    object *t = &my_object;
    cout << t->get_type();
    

    When you’re using non-virtual functions, the type of the pointer t is used at compile time to determine which method to call. When you’re using virtual functions, information inside the object is used at runtime during virtual dispatch to decide which function to call. Since your object is not correctly constructed at the time of the call (since it has already been destructed), the result is strictly undefined but ends up calling the wrong function.

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

Sidebar

Related Questions

Alright, so I'm currently working on a game and ran into a memory issue
Alright so i have been working on this Dynamic load of a spinner from
Alright, currently I have my SWF hitting a php file that will go and
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright, so all modals already have an image at the top right to close
Alright, here is my problem i'm trying to solve. I have an index page
Alright, I've got a quick question. I'm currently working with a legacy database, so
Alright, I'm currently working to create on an account mainpage a applet to show
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright. So I have a very large amount of binary data (let's say, 10GB)

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.