Hello, help to implement a recursive algorithm for the output of all elements. Example output: [1,2.0, false, [1,2], 4.2, [1, [2], true]], where [] is a container. Perhaps you need to start from the top, display the contents of the container, if the container again meets deepen, so to the end. But where to start? I can’t even imagine how to overload the methods for the element and the container

upd: OP went to sleep, will be in the morning

class base { public: virtual bool Equals(base *n) = 0; virtual void ToString(char* bufer) = 0; virtual ~base() {} }; class element : public base { public: union { int iVar; float fVar; bool bVar; }; public: element(int var) { iVar = var; } element(float var) { fVar = var; } element(bool var) { bVar = var; } bool Equals(base* n) { element* p = dynamic_cast<element*>(n); if (bVar == p->bVar || fVar == p->fVar || iVar == p->iVar) { return true; } else { return false; } } bool operator == (element* n) { if (bVar == n->bVar || fVar == n->fVar || iVar == n->iVar) { return true; } else { return false; } } void ToString(char* buffer) { } }; class container : public base { public: private: base*** array; int order; int ni; int nj; //последний элемент public: container(int ord) { ni = 0; nj = 0; order = ord; array = new base**[order]; for (int i = 0; i < order; i++) { array[i] = new base*[order]; for (int j = 0; j < order; j++) { array[i][j] = NULL; } } } ~container() { for (int i = 0; i < order; i++) { if (array[i]) { delete[] array[i]; } delete[] array; array = NULL; order = 0; } } bool Add(base *n) { //добавление элемента в конец array[ni][nj] = n; if (nj != order - 1) { nj++; return true; } else if (nj == order - 1 && ni != order - 1) { nj = 0; ni++; } else if (nj == order - 1 && ni == order - 1) { return false; } return false; } bool Equals(base* n) { container* p = dynamic_cast<container*>(n); bool full = true; for (int i = 0; i < order; i++) { for (int j = 0; j < order; j++) { if (array[i][j] != p->array[i][j]) { full = false; } } } return full; } void ToString(char* buffer) { }; }; 

    1 answer 1

    Do you really need such an idea - with a union , a triple pointer base*** , etc.?

    To write the recursive output is quite simple. Check your base . If it is an element , just output it and return. If this is container - output [ , then perform a walk through all the elements in it, calling for each output function. At the end - ] . You will understand the commas yourself :)

    How do you determine what exactly is from the union now in your element and how to display it - I do not know, unless you add an additional field.

    In general, all this is reminiscent of attempts to tie different parts of a bicycle with ropes in the hope of getting a motorcycle. For example, first answer the questions:

    • How is the use of the union justified?

    • Why are the data in the class made public ?

    • What happens if, when casting dynamic_cast your pointer is not what you expected?

    • Is base***array necessary? Maybe you should use some standard container?

    • You do not find unreliable checking if (bVar == p->bVar || fVar == p->fVar || iVar == p->iVar) ? What if the low byte matches for different integers, for example? ...

    • union - the condition of the job. Yes, classes should be in private. base *** array is a two-dimensional array. STL Biblioteca cannot be used ... you need to think about the rest) - dahbka
    • Classes in private will not work, I use them in Maine - dahbka