Solved

I want a stronger typedef

Posted on 2000-04-12
6
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 copy an image file into clipboard C/C++? 1 255
Precision Problem in C++ 7 67
Gaming Software 1 39
Need some help with design 17 48
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

752 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