1

I have the following code below which does work except that the line POINTEE* pointee[10]; is static and I want to make it dynamic whenever I create a class so it can be any size.

#include <iostream>

class POINTEE
{
    private:

        int index;

    public:

    POINTEE(){}
    POINTEE(int index)
    {
        this->index = index;
    }
    ~POINTEE(){}
    void print_index()
    {
        std::cout<<index<<std::endl;
    }
};
void fill_element(POINTEE* &pointee, int index)
{
    pointee = new POINTEE(index);
}
int main()
{
    POINTEE* pointee[10];//I want to declare this within a class with a variable size instead of 10

    for(int index = 0; index < 10; index++)
        pointee[index] = NULL;

    for(int index = 0; index < 10; index++)
    {
        POINTEE* temp_pointee;
        fill_element(temp_pointee, index);
        pointee[index] = temp_pointee;
    }

    for(int index = 0; index < 10; index++)
        pointee[index]->print_index();

     for(int index = 0; index < 10; index++)
        delete pointee[index];

    return 0;
}

I don't want to use std::vector mainly because I'm trying to design my own data container. I also tried doing

#include <iostream>

class POINTEE
{
    private:

        int index;

    public:

    POINTEE(){}
    POINTEE(int index)
    {
        this->index = index;
    }
    ~POINTEE(){}
    void print_index()
    {
        std::cout<<index<<std::endl;
    }
};
void fill_element(POINTEE* &pointee, int index)
{
    pointee = new POINTEE(index);
}
int main()
{
    POINTEE* pointee;// I changed this
    pointee = new POINTEE[10];//and this and also deleted pointee below

    for(int index = 0; index < 10; index++)
        pointee[index] = NULL;

    for(int index = 0; index < 10; index++)
    {
        POINTEE* temp_pointee;
        fill_element(temp_pointee, index);
        pointee[index] = temp_pointee;
    }

    for(int index = 0; index < 10; index++)
        pointee[index]->print_index();

     for(int index = 0; index < 10; index++)
        delete pointee[index];

    delete [] pointee;//I added this which maybe totally stupid!

    return 0;
}

but that made other errors appear:

C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp||In function 'int main()':|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|38|error: invalid conversion from 'POINTEE*' to 'int'|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|38|error:   initializing argument 1 of 'POINTEE::POINTEE(int)'|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|42|error: base operand of '->' has non-pointer type 'POINTEE'|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|45|error: type 'class POINTEE' argument given to 'delete', expected pointer|
||=== Build finished: 4 errors, 0 warnings ===|
pandoragami
  • 5,387
  • 15
  • 68
  • 116
  • "array of pointers" **begs for** being implemented as `vector >`... –  Jun 15 '13 at 20:07
  • Surely theres another way. I know its easy to implement this as a std::vector but why not using standard pointers? – pandoragami Jun 15 '13 at 20:09
  • 2
    [this](http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap) et al. –  Jun 15 '13 at 20:11
  • If you want to emulate the dynamic size of std::vector, you'll have to write code that, when the array is filled, copies the content of the array into a new array of larger size which effectively replaces the old array. – A.B. Jun 15 '13 at 21:04
  • @A.B. But how would you create the array in the first place for the specific size and what about push_back()? – pandoragami Jun 15 '13 at 22:10
  • @H2CO3 Not sure what the link is for. Are you saying theres a performance advantage with vectors? – pandoragami Jun 15 '13 at 22:11
  • @lost_with_coding Nah, I'm saying that preferring premature optimization over safety is evil. –  Jun 15 '13 at 22:12

1 Answers1

1

I would definitely use a vector myself unless you really want to make your own vector class, but here are some issues with your code:

The following creates a pointer pointee that points to an array of 10 POINTEE objects. It does not point to pointers to POINTEE objects.

POINTEE* pointee;// I changed this
pointee = new POINTEE[10];//and this and also deleted pointee below

If you change the lines to the following:

POINTEE** pointee;
pointee = new POINTEE*[10];

then your code is at least a lot closer to working. I didn't look too closely, but I think that the rest of your code was mostly compilable.

Justin Peel
  • 46,722
  • 6
  • 58
  • 80