klopter
asked on
I want a stronger typedef
I have three parallel vectors. (i.e. Read[i] corresponds
to Full[i] and Sparse[i]).
I would like to create a type that can only be used
to index these three vectors. Since vectors are
idnexed by ints by definition I can't prevent these
vectors from being indexed by a straight int type
(at least I don't think that I can). But, I am still hoping
that I can at least create a type that can only
be used to index these vectors.
If I can't., I can't.
thanks,
Ken
to Full[i] and Sparse[i]).
I would like to create a type that can only be used
to index these three vectors. Since vectors are
idnexed by ints by definition I can't prevent these
vectors from being indexed by a straight int type
(at least I don't think that I can). But, I am still hoping
that I can at least create a type that can only
be used to index these vectors.
If I can't., I can't.
thanks,
Ken
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Meaning that I can use a typedef now
>> but could migrate to a stronger type system
If you plan for it, it would probably be an easy enough change.
However, it probably isn't that hard to do now too.
// Class uses to index the array. All functions are inline so there should
// not be much overhead (if any) compared to regular int indexes.
class ArrayIndex
{
int Index;
public:
// Pitfall prevention. Note use of explicit. Otherwise
// the compiler will convert ints to ArrayIndexes automatically
// if needed, and thus an int could be used an in index
// into one of these arrays.
explicit ArrayIndex(int i) : Index(i) {};
int GetIndex() const { return Index; } ;
// Possibly other functions if needed, like a SetIndex().
};
continues
>> but could migrate to a stronger type system
If you plan for it, it would probably be an easy enough change.
However, it probably isn't that hard to do now too.
// Class uses to index the array. All functions are inline so there should
// not be much overhead (if any) compared to regular int indexes.
class ArrayIndex
{
int Index;
public:
// Pitfall prevention. Note use of explicit. Otherwise
// the compiler will convert ints to ArrayIndexes automatically
// if needed, and thus an int could be used an in index
// into one of these arrays.
explicit ArrayIndex(int i) : Index(i) {};
int GetIndex() const { return Index; } ;
// Possibly other functions if needed, like a SetIndex().
};
continues
class Array
{
vector<Widget> A;
public:
void Insert(const Widget &W) { A.push_back(W); };
// probably need other functions for inserting/deleteing etc.
// Non-constant index operator. Takes an ArrayIndex, not an int.
W &operator [](const ArrayIndex &Index)
{
return A[Index.GetIndex()];
};
// Constant index operator. Takes an ArrayIndex, not an int.
const W &operator [](const ArrayIndex &Index) const
{
return A[Index.GetIndex()];
};
};
{
vector<Widget> A;
public:
void Insert(const Widget &W) { A.push_back(W); };
// probably need other functions for inserting/deleteing etc.
// Non-constant index operator. Takes an ArrayIndex, not an int.
W &operator [](const ArrayIndex &Index)
{
return A[Index.GetIndex()];
};
// Constant index operator. Takes an ArrayIndex, not an int.
const W &operator [](const ArrayIndex &Index) const
{
return A[Index.GetIndex()];
};
};
you would use it like
Widgit W1;
Widgit W2;
ArrayIndex AI(1); // Index for 2nd item.
Array A;
A.insert(W1); // Insert 1st item
A.insert(W2); // Insert 2nd item.
W3 = A[AI]; // Get 2nd item.
W3 = A[ArrayIndex(1)]; // Also gets 2nd item.
W3 = A[1]; // Compiler error!
Widgit W1;
Widgit W2;
ArrayIndex AI(1); // Index for 2nd item.
Array A;
A.insert(W1); // Insert 1st item
A.insert(W2); // Insert 2nd item.
W3 = A[AI]; // Get 2nd item.
W3 = A[ArrayIndex(1)]; // Also gets 2nd item.
W3 = A[1]; // Compiler error!
ASKER
Great. For now I will use a typedef with confidence
that I can switch a systemlike you describred if
warranted later.
Thanks,
Ken
that I can switch a systemlike you describred if
warranted later.
Thanks,
Ken
ASKER
like. And, especially if it is something
that I could add later. Meaning
that I can use a typedef now but could
migrate to a stronger type system if
the program gets large enough and
important enough to warrant it.
Ken