Solved

I want a stronger typedef

Posted on 2000-04-12
6
198 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:klopter
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 75 total points
ID: 2708116
There is a way, but I'm not sure you will like it.  (Its seems like a lot of work compared to the small amount of extra safety you might get.)

you can create array classes for these 3 arrays.  (or just create a wrapper class for them, that has an array or vector as a data member.  i.e. you might not have to create a full-featured array class.)   Then define some sort of index class that has integer data member tahat stores the array index value.  Then for the operator [] functions for your array classes, specify a parameter if this index class type, rather than an int type.

I can provide some more details if you want to do this.  (There is a pitfall or two that I shoudl cover.)
0
 

Author Comment

by:klopter
ID: 2708154
I'd be interested to see what this looks
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 2708212
>> 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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
ID: 2708238
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()];
    };

};

0
 
LVL 22

Expert Comment

by:nietod
ID: 2708257
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!


 
0
 

Author Comment

by:klopter
ID: 2708323
Great.  For now I will use a typedef with confidence
that I can switch a systemlike you describred if
warranted later.

Thanks,
  Ken
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 106
C++ question 3 70
Arduino EDI - Programming language 3 103
C++ :Change value from  DisableCMD registry 4 65
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question