Solved

I want a stronger typedef

Posted on 2000-04-12
6
194 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

773 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