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
need help : pointer to an array of int
hi
i'm quite desperate as of this moment,
could someone please help me correct the following code:
As you're using C++, make aSave a class:
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
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
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!
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!
You could end up with something like this:
But the class solution is better I think.
typedef int aSave[3], *aSavePtr;...aSavePtr Save = new aSave;
But the class solution is better I think.
Now, if you want to get funky with this, then you could add some template magic:
The above lets us extend and alter the array class by defining a new traits class:
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
#include <algorithm>// the array class templatetemplate <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 [] functionclass 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 integerstypedef 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 100typedef 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement