Sign in to follow this  
Neonate

need help : pointer to an array of int

Recommended Posts

Neonate    150
hi i'm quite desperate as of this moment, could someone please help me correct the following code:
typedef int aSave[3];
...
aSave *_Save;
...
_Save=new aSave[];
my thoughts are: 1. the third line is definitely wrong(should be *_Save, not just _Save), but if i change it to "*_Save=new aSave[];" it says: ...error C2440: '=': 'aSave (*)' cannot be converted to 'int [3]' 2. i need the typedef, as i'm using aSave very often 3. i want a pointer to an array, not an array of pointers thanks

Share this post


Link to post
Share on other sites
Skizz    794
As you're using C++, make aSave a class:

class aSave
{
public:
int &operator [] (int index)
{
return array [index];
}

private:
int array [3];
};

You can also add bounds checking to the operator []. You'll need to add extra stuff to the class to make it really useful (constructor, assignment, etc).

Skizz

Share this post


Link to post
Share on other sites
Neonate    150
Thanks for feedback,
I wanted to make it as simple as possible, but hey, that's a good solution. It's much clearer than the array/pointer mess.

A bit more code to type but who cares.

Big Thanks!

Share this post


Link to post
Share on other sites
Skizz    794
Now, if you want to get funky with this, then you could add some template magic:

#include <algorithm>

// the array class template
template <class T, int size, class trait = default_array_trait>
class generic_array
{
public:
generic_array (void)
{
std::fill (m_array, &m_array [size], 0);
}

T &operator [] (int index)
{
return m_array [trait::check_index (index, size)];
}

private:
T
m_array [size];
};

// this class is used to check the index used in the generic_array operator [] function
class default_array_trait
{
public:
static int check_index (int index, int size)
{
return index >= 0 && index < size ? index : 0;
}
};

// define a type that contains 3 integers
typedef generic_array<int, 3> simple_array;

void f1 (void)
{
// example of using above type
simple_array
an_array;

an_array [0] = 42;
}


The above lets us extend and alter the array class by defining a new traits class:

template <int lower_bounds>
class bounded_array_trait
{
public:
static int check_index (int index, int size)
{
return index >= lower_bounds && index < (lower_bounds + size) ? index - lower_bounds : 0;
}
};

// define a type that contains 10 integers starting at index 100
typedef generic_array <int, 10, bounded_array_trait <100> > bounded_array;

void f2 (void)
{
// example of using above type
bounded_array
an_array;

an_array [100] = 42;
}



You can also change the types held in the array. This is getting close to what std::vector and similar STL types do - std::vector does dynamic resizing. Quite often you'll find that the STL already implements types that do what you want.

Skizz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this