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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:19:45+00:00 2026-06-06T19:19:45+00:00

Possible Duplicate: Learning C++: polymorphism and slicing Array of polymorphic base class objects initialized

  • 0

Possible Duplicate:
Learning C++: polymorphism and slicing
Array of polymorphic base class objects initialized with child class objects

I’ve integrated object oriented classes into my OpenGL application. The base class has a function called Tick that should get called every tick (obvious) with a parameter representing the delta time. Here’s how it looks like (without the irrelevant things for this question).

Headers:

class Object
{
public:
    virtual void Tick(float DeltaTime);
}
class Controller : public Object
{
public:
    virtual void Tick(float DeltaTime);
}

Then I have a class called Engine that contains the main loop, initializations and disposal (to separate it from the window creation).
In that class I need to keep track of all the objects in the game, so I created an array:
Object* Objects = new Object[10]; // for now max 10 objects

Then to call the tick function I iterated trough the array:

for (unsigned int c = 0; c < 10; c++)
    Objects[c].Tick(delta);

Being delta calculated before the loop.

The problem is that if I assign one of the objects in the array to a Controller (for example) the Tick function called is always the one that’s in Object and not in the class that’s actually stored.

I even tried type casting (each class has a string to identify it’s type), with no avail.

I’m pretty sure that the answer is something obvious (like the answer to my last questions), but I can’t find the answer.

Thank you for your time. 🙂

  • 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-06T19:19:47+00:00Added an answer on June 6, 2026 at 7:19 pm

    You have encountered the slicing problem.

    The solution is to not store polymorphic objects by value in an array; use (smart) pointers instead.

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

Sidebar

Related Questions

Possible Duplicate: Learning C++: polymorphism and slicing This is building off a question I
Possible Duplicates: C++: Delete this? Object-Oriented Suicide or delete this; I'm learning C++ by
Possible Duplicate: What is the best Evaluation Kit for Learning Embedded C/C++ Development? I'm
Possible Duplicate: SQL: What’s the difference between HAVING and WHERE? i am learning sql
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Learning about LINQ Hi everyone, I just want to understand what exactly
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: What does ‘unsigned temp:3’ means I'm learning some kernel code, and came
Possible Duplicate: Is it a good idea to learn JavaScript before learning jQuery? I
Possible Duplicate: How to stop C++ console application from exiting immediately? So im learning

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.