HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Multi level list based on array of void pointers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
multilevelarraypointersbasedlistvoid

Problem

I have written code, but I don't like it, it seems very ungeneralized. I want to make it more abstract and universal and independent of type.

I have the task to write programm, which can operate with multi level list. List should be done on array of void pointers. Data structure should be roughly like on the picture:

I have made this on classes. I have separate class pointerArray, which operates with array of void pointers. But I didn't hit upon the idea how to make generalized class (which doesn't depend of structure type), namely how to do delete function which doesn't depend on structure type. So I have made it template class.

But the main thing, which I don't like is: if I need to add element to the lowest level of list I should step by step cast types to reach lowest level.

For this program maybe it isn't so important, but I have to make same list for more than 5 levels.

So, please, look through, comment and suggest what to change or how to realise this task in another way. Here this sources on github
WITHOUT USING STANDARD CLASSES (like ` and so on)
Here is some part of code
pointerArray.h

``
#ifndef POINTERARRAY_H
#define POINTERARRAY_H
#define DEF_SIZE (10)
#define DELTA (4)

#include
#include
#include
#include
#include

using namespace std;

typedef
int (TCmpFunc)(void, string);

template
class pointerArray {
public:
pointerArray(int initialSize = DEF_SIZE){
this->start = new void [initialSize]; / запрашиваем память для нового массива*/
this->initArr(initialSize);
}
void showList();
bool delFromList(int posDel);
int findElList(string key, TCmpFunc cmpF,bool &findOK);
void addToSort(void* pnew, int posAdd);
void replaceSort(int delPos, void* newEl, int inPos);
void clearAll();
bool delAllList(bool delPointers);
void expandDef(int sizeOld, int incSize);
void** getStart() {
return this->start;
};
int getSize() {
return this->size;
};
int

Solution

This looks a lot like c with classes rather than c++.

  • If you are using void pointers in C++, you are probably doing something wrong. You pointerArray class is templated, but you don't use the templates at all.



  • A class name should begin with a capital letter.



  • Use a function object (functor) instead of a function pointer.



  • You don't have a destructor for pointerArray. If an exception is thrown, you'll probably have a memory leak.



Here's a basic idea of what you may want to do instead:

#define DEF_SIZE 10
#define DELTA 4

template 
struct SampleCompareFunctor
{
    bool operator () (const T &lhs, const T &rhs) const {
        return lhs 
class Array
{
public:
    Array (const size_t initial_size = DEF_SIZE) ;
    ~Array () ;

    void insertValue (const size_t index, const T &val) ;
    T removeValue (const size_t index) ;

    // Getters, so you could do Array a ;  a [0] ....
    T& operator [] (const size_t index) ;
    const T& operator [] (const size_t index) const ;

    template 
    void sort () ;

//...

private:
    T *m_data ;
};

template 
Array ::Array (const size_t initial_size) : m_data (new T [initial_size])
{
}

template 
Array ::~Array ()
{
    delete [] m_data ;
    m_data = nullptr ; // Or NULL if you don't have c++11
}

template 
T& Array ::operator [] (const size_t index)
{
    return m_data [index] ;
}

template 
const T& Array ::operator [] (const size_t index) const
{
    return m_data [index] ;
}

template 
template 
void Array ::sort ()
{
    // Implement your sorting function here.
}


This is just a partial review. I haven't looked at the rest yet.

Code Snippets

#define DEF_SIZE 10
#define DELTA 4

template <typename T>
struct SampleCompareFunctor
{
    bool operator () (const T &lhs, const T &rhs) const {
        return lhs < rhs ;
    }
};

template <typename T>
class Array
{
public:
    Array (const size_t initial_size = DEF_SIZE) ;
    ~Array () ;

    void insertValue (const size_t index, const T &val) ;
    T removeValue (const size_t index) ;

    // Getters, so you could do Array a ;  a [0] ....
    T& operator [] (const size_t index) ;
    const T& operator [] (const size_t index) const ;

    template <typename CompareFunctor>
    void sort () ;

//...

private:
    T *m_data ;
};

template <typename T>
Array <T>::Array (const size_t initial_size) : m_data (new T [initial_size])
{
}

template <typename T>
Array <T>::~Array ()
{
    delete [] m_data ;
    m_data = nullptr ; // Or NULL if you don't have c++11
}

template <typename T>
T& Array <T>::operator [] (const size_t index)
{
    return m_data [index] ;
}

template <typename T>
const T& Array <T>::operator [] (const size_t index) const
{
    return m_data [index] ;
}

template <typename T>
template <typename CompareFunctor>
void Array <T>::sort ()
{
    // Implement your sorting function here.
}

Context

StackExchange Code Review Q#32958, answer score: 2

Revisions (0)

No revisions yet.