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

  • Home
  • SEARCH
  • 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 795767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:31:53+00:00 2026-05-14T22:31:53+00:00

I have the following code #include <iostream> #include <vector> class Entity { public: virtual

  • 0

I have the following code

#include <iostream>
#include <vector>

class Entity {
public:
    virtual void func() = 0;
};

class Monster : public Entity {
public:
    void func();
};

void Monster::func() {
std::cout << "I AM A MONSTER" << std::endl;
} 

class Buddha : public Entity {
public:
    void func();
};

void Buddha::func() {
std::cout << "OHMM" << std::endl;
}

int main() {
const int num = 5;  // How many of each to make
std::vector<Entity*> t;

for(int i = 0; i < num; i++) {
    Monster m;
    Entity * e;
    e = &m;

    t.push_back(e);
}

for(int i = 0; i < num; i++) {
    Buddha b;   
    Entity * e;
    e = &b;

    t.push_back(e);
}

for(int i = 0; i < t.size(); i++) {
    t[i]->func();
}

return 0;
}

However, when I run it, instead of each class printing out its own message, they all print the “Buddha” message. I want each object to print its own message: Monsters print the monster message, Buddhas print the Buddha message.

What have I done wrong?

  • 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-14T22:31:53+00:00Added an answer on May 14, 2026 at 10:31 pm

    You need to allocate the objects from the heap with ‘new’. What’s happening here is that you’re creating temporary objects, taking the pointer to those objects, and then those objects are being destroyed. Yes, this is differerent from many other languages. 🙂

    Instead, try:

    int main() {
       const int num = 5;  // How many of each to make
       std::vector<Entity*> t;
    
       for(int i = 0; i < num; i++) {
          Monster* m = new Monster;
          t.push_back(m);
       }
    
       for(int i = 0; i < num; i++) {
          Buddha* b = new Buddha;
          t.push_back(b);
       }
    
       for(int i = 0; i < t.size(); i++) {
          t[i]->func();
       }
    
       // This is very important!
       for(int i = 0; i < t.size(); i++) {
          delete t[i];
       }
    
       return 0;
    }
    

    When you see weird behavior like this, check to see the contents of the actual vector. You’d find that all of your slots had the same value, which is the spot on the stack that was holding the temporary monster then the temporary buddha.

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

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using try-catch to control the flow of the program is… May 16, 2026 at 11:02 am
  • Editorial Team
    Editorial Team added an answer Turns out I have a book that tells me what… May 16, 2026 at 11:02 am
  • Editorial Team
    Editorial Team added an answer You can pass a bool to .toggle() to tell it… May 16, 2026 at 11:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
I have the following code fragment #include <iostream> #include <ostream> #include <vector> using namespace
Have a look at the following code you are not required to read the
I am new to Emacs, and I have the following code as a sample.
Let's say I have a base class Animal from which a class Cow inherits,
I have to write a program that performs highly computationally intensive calculations. The program
I have a source container of strings I want to remove any strings from
I wanted to write a higher order function filter with C++. The code I
I have some misunderstanding about the gslice function. Definition from MSDN states: gslice defines
I have a basic solution file (.sln) where I was able to reproduce a

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.