0

I need an array that stores unique objects, and preferably, I would like to store Super-class instances of the Sub - class of what the array was made for... this is the code I have so far:

The reason why I need this is so that I can loop through the items in the array (not literally looping, looping in an abstract way) to update motors and such and such (I would be using this code for a robot)

#include <iostream>

class Task
{
public:

    int UpdateTask()
    {
        //to be overriden by superclass.
    }
};

class SuperTask: Task;
{
private:

float SomeInputVariable;
bool Wait;

public:

    SuperTask(float some_input_variable, bool wait = true)
    {
        SomeInputVariable = some_input_variable;
        Wait = wait;
        //the constructor would mainly be for setting values.
    }

    int UpdateTask()
    {

    if (SomeExitCondition)
    {
        //stop motors etc.
        return -1;
        //0 will be the excape value.
    }
    
    //update motors etc.

    if (Wait)
    {
        return 0;
        // 1 will be the value that tells the loop to not go any further
        // until this task is done (returns 0)
    }

    return 1;


};

Task Tasks[] = {

SuperTask t1(100, true),

};

int main()
{
    while(true)
    {
        //do some looping stuff.
        //here I would need to call the UpdateTask() method on each task.
    }

}

and right where I declare t1, visual studio (2019) says "type name not allowed"

I am unsure how to accomplish what I am trying to do, is there a way to do what I am trying to do that will work?

Pi_way
  • 1
  • 1
  • 2
    Setting aside the syntax error, you cannot store subclasses in arrays of superclasses, full stop. C++ does not work this way. You must use pointers. – Sam Varshavchik Sep 12 '20 at 23:59
  • 1
    arrays can only store objects of one type. A `SuperTask` is more than a `Task`, it doesn't fit into an array of `Task`s. Read about "object slicing" – 463035818_is_not_an_ai Sep 13 '20 at 00:10
  • 1
    Helpful reading: [What is object slicing?](https://stackoverflow.com/questions/274626/what-is-object-slicing) – user4581301 Sep 13 '20 at 00:20

0 Answers0