Solved

I want a stronger typedef

Posted on 2000-04-12
6
206 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

624 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