Jump to content
  • Advertisement
Sign in to follow this  
Sh3rlok

Simple Object Array Question (C++)

This topic is 3891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So my university exclusively teaches Java (a class on ada/prolog, but that's it). So I decided I needed to learn C++ on my own, so I've been going back and doing old java projects in C++. The one I'm doing now is a simple resizeable stack implementation. It's the first of these projects I'm attempting, and I'm stuck on an array of objects. I decided to just jump into it and use Templates, pointers, the whole 9 yards. So here's my array declaration:
T* base;  //our starting array, can be resized
and then later on in the constructor:
base = new T[max_size];    //init our array
which I think is the correct way to make an array of pointers. However I'm having issues compiling this method:
    /*
        Pushes an object onto our stack
    */
    void push(T* object){

        //check our stacks size
        if(curr_index >= max_size)
            resize(max_size + 3);

        base[curr_index++] = object;
    }
I know it's a dumb thing, but I can't seem to figure out nor could I find an example that I could understand. Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
You are creating an array of T-objects. Your push method wants to assign a pointer-to-T to one of those objects.
This should fix it:

T** base;

base = new T*[max_size];

Share this post


Link to post
Share on other sites
What error are you getting? If you want an array of pointers you would need to say: T** base = new T*[max_size];

Share this post


Link to post
Share on other sites
That did it, thanks a ton guys.

my understanding was that T* base[] would create an array of T*'s (pointer to T objects).

Thanks for the help.

Share this post


Link to post
Share on other sites
A pointer to T is roughly equivalent to an array-of-T variable. Not to an array-of-pointers-to-T. So:

T array_of_T[15];
T* pointer_to_T = array_of_T;
//which is equivalent to
pointer_to_T = &(array_of_T[0]);

Share this post


Link to post
Share on other sites
awesome thanks for the clarification.

I've got another issue, I figured I'd just use this thread since it's related.

My function to resize my array is being a bit...wonky. I found a great sample in an old post on these forums, but it seems to just be copying the last address to the entire new array. Here's some sample output when it resizes:

Here are the values

i[0] Copying: 6 to 6
i[1] Copying: 6 to 6
i[2] Copying: 6 to 6
i[3] Copying: 6 to 6
i[4] Copying: 6 to 6


And the addresses


i[0] Copying: 0x22ff48 to 0x22ff48
i[1] Copying: 0x22ff48 to 0x22ff48
i[2] Copying: 0x22ff48 to 0x22ff48
i[3] Copying: 0x22ff48 to 0x22ff48
i[4] Copying: 0x22ff48 to 0x22ff48



Here's the code:


/*
resizes the array to the desired size
*/
void resize(int size){

int newSize;

//check for size issues
if(max_size > size)
return; //doign anything to an equal or smaller sized array would be pointless
else
newSize = size;

T** temp = new T*[newSize]; //make our array

for(int i = 0; i < max_size; i++) {
temp = base;
cout << "i[" << i << "] Copying: " << base << " to " << temp << endl;
}

max_size = newSize; //reset our current max size

//gonna delete the old array, and point it to the new one
delete[] base;

base = temp;
}


The current call to resize is max_size+3 and the initial base array is a size of 5.

Any insight would eb appreciated

Share this post


Link to post
Share on other sites
Oh maybe I should clarify, the array currently holds numbers 1-5 with the number 6 being the newest addition to the array (however it's not yet added since the array is of size 5 and needs to be resized first)

so the correct output should be 1-5 regardless of where the print is. Unless I'm seeing it completely wrong.

Thanks though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sh3rlok
Oh maybe I should clarify, the array currently holds numbers 1-5


Are you sure about that?

Share this post


Link to post
Share on other sites
/*
resizes the array to the desired size
*/
void resize(int size){

int newSize;

//check for size issues
if(max_size > size)
return; //doign anything to an equal or smaller sized array would be pointless
else
newSize = size;

T** temp = new T*[newSize]; //make our array

for(int i = 0; i < max_size; i++) {
temp = base;
cout << "i[" << i << "] Copying: " << base << " to " << temp << endl;
}

max_size = newSize; //reset our current max size

//gonna delete the old array, and point it to the new one
delete[] base;

base = temp;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!