Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

c++ - How should i define the function getSubClassElements()?

Ok so I have an abstract base class which has a virtual function getTitle() which is also used in all subclasses. The subclasses also have their own unique member functions and variables. I then have a function where I create a dynamic array of the type base class pointers which works fine since all of the subclasses inherit from the base class. My issue is however when I later need to create vectors for each subclass type and push_back() all of the values from each subclass to the corresponding vector. I don't know how to do this since they know are of the type *baseClass.

note start values: size = 0; BaseClass** array = new BaseClass*[0]

void add(BaseClass* base)
{
    size ++;
    BaseClass** temp_array = new BaseClass*[size];

    for(int i = 0; i < size - 1; i++)
    {
        temp_array[i] = array[i];
    }
    array[size - 1] = base;
    delete[] array;
    array = temp_array;
}

void addSubClass(const SubClass& sub)
{
    SubClass* copy = new SubClass(sub);
    add(copy);
    delete copy;
}

std::vector<SubClass> getSubClassElements() const
{
    std::vector<SubClass> vector;

    //HERE IS WHERE I NEED HELP

    return vector;
}
question from:https://stackoverflow.com/questions/65851267/how-should-i-define-the-function-getsubclasselements

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...