Solved

I want a stronger typedef

Posted on 2000-04-12
6
189 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now