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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:20:31+00:00 2026-06-14T13:20:31+00:00

Possible Duplicate: Overriding parent class’s function I’m struggling with calling a virtual function in

  • 0

Possible Duplicate:
Overriding parent class’s function

I’m struggling with calling a virtual function in C++.

I’m not experienced in C++, I mainly use C# and Java so I might have some delusions, but bear with me.

I have to write a program where I have to avoid dynamic memory allocation if possible. I have made a class called List:

template <class T> class List {
public:
    T items[maxListLength];
    int length;


    List() {
        length = 0;
    }

    T get(int i) const {
        if (i >= 0 && i < length) {
            return items[i];
        } else {
            throw "Out of range!";
        }
    };

    // set the value of an already existing element
    void set(int i, T p) {
        if (i >= 0 && i < length) {
            items[i] = p;
        } else {
            throw "Out of range!";
        }
    }

    // returns the index of the element
    int add(T p) {
        if (length >= maxListLength) {
            throw "Too many points!";
        }
        items[length] = p;
        return length++;
    }

    // removes and returns the last element;
    T pop() {
        if (length > 0) {
            return items[--length];
        } else {
            throw "There is no element to remove!";
        }
    }
};

It just makes an array of the given type, and manages the length of it.

There is no need for dynamic memory allocation, I can just write:

List<Object> objects;
MyObject obj;
objects.add(obj);

MyObject inherits form Object. Object has a virtual function which is supposed to be overridden in MyObject:

struct Object {
    virtual float method(const Input& input) {
        return 0.0f;
    }
};

struct MyObject: public Object {
    virtual float method(const Input& input) {
        return 1.0f;
    }
};

I get the elements as:

objects.get(0).method(asdf);

The problem is that even though the first element is a MyObject, the Object’s method function is called. I’m guessing there is something wrong with storing the object in an array of Objects without dynamically allocating memory for the MyObject, but I’m not sure.

Is there a way to call MyObject’s method function? How? It’s supposed to be a heterogeneous collection btw, so that’s why the inheritance is there in the first place.

If there is no way to call the MyObject’s method function, then how should I make my list in the first place?

Also I have no access to libraries outside of math.h and stdlib.h, so vector is not available for example.

  • 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-14T13:20:32+00:00Added an answer on June 14, 2026 at 1:20 pm

    When you do this:

    objects.add(obj);
    

    you are adding a copy of the Object part of the MyObject to the list, so it is no longer a MyObject.

    You might be tempted to try doing this:

    int add(T const &p) {
        if (length >= maxListLength) {
            throw "Too many points!";
        }
        items[length] = p; // now the problem is here
        return length++;
    }
    

    but now the copy of the Object part of p happens during the assignment.

    To make the list be heterogeneous, it is going to have to be a list of pointers, but you also wanted to avoid dynamic memory allocation. You can avoid dynamic memory allocation if you are careful:

     Object obj1;
     MyObject obj2;
     List<Object*> object_ptrs;
     object_ptrs.add(&obj1);
     object_ptrs.add(&obj2);
     object_ptr.get(1)->method(input);
     object_ptr.get(0)->method(input);
    

    but again, you have to be very careful. The list is now pointing to the two objects on the stack. If you return from this function, those two objects will be destroyed. Note that I’ve purposefully put the list of object pointers after the objects, so that the list will get destroyed before the objects, so the list won’t be left pointing to garbage. However, if you return a copy of the list, you would still have a problem.

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

Sidebar

Related Questions

Possible Duplicate: Overriding vs Virtual In C++, whether you choose to use virtual or
Possible Duplicate: Overriding equals and hashCode in Java If I have class A {
Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: operator overloading and overriding in java I want to know if it
Possible Duplicate: Why doesn't Java allow overriding of static methods? Is there any legitimate
Possible Duplicates: Overriding vs Virtual How am i overriding this C++ inherited member function
Possible Duplicate: Overriding the power button in Android Is there any possible way I
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Overriding static variables when subclassing I have a set of classes that

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.