Hi,

I have class which has two bool vectors:

typedef vector<bool> my_bool_vector;
class MyClass{
private:
my_bool_vector bvec;
my_bool_vector another_bvec;
public:
explicit MyClass(unsigned int size) : 
  bvec(size, false), 
  another_bvec(size, false) {
}
};
MyClass MyObject (10);

I want to be able to access bvec and another_bvec in the following way, using operator [] :
When I read MyObject[n], it should read bvec[n], and when I write to MyObject[n], it should write to another_bvec[n].

Is this even possible by overloading operator []? I have tried writing two versions, one that returns bool& and another that returns const bool, but g++ complains.

If this is not possible with operator [], can someone think of another efficient way?
My other option is use read(n)/write(n) interface functions, but they are cumbersome.

Thanks.

have you tried syntax similar to this:

MyObject.bvec[n];

It should work as long as n is less than the size of bvec, which in the code provided would anything 0-9 inclusive.

Thanks Lerner.

Well, bvec and another_bvec are private to MyClass, and I don't want any external code to directly access them. In fact, external code should not even be aware that there are two vector<bool> inside. The functionality of the class MyClass is defined that writes should happen to another_bvec, and reads should happen from bvec.

Think in terms of D flipflops. You always write to the D input and always read from the Q output. The transfer of input to output, or in my case, another_bvec to bvec, should only happen when a function MyClass::transfer() {not shown in the original code} is called.

How about including public functions read() and write(). Both should take an int parameter. Then you can call MyObject.read(n) and MyObject.write(n). I don't see how you can overload [] to both read and write; how will compiler/user know whether you are trying to read or write if all you use is []?

Yea, using read/write functions was my other (rather, only) choice of implementation. I was hoping to figure out a smarter way.

The compiler/user may be able to tell the difference between read and write by looking at which side of = the [] appears. If the operator[] is used on the RHS, it is const bool, and if it is on the LHS it is bool& (a reference). Basically I wanted to have two implementations of [] so that the compiler can figure out which one to pick based on the usage of the the reference.

Hmm. May be I can simply define two implemenations: one that returns a reference and another that returns a const reference. (Earlier I was trying one with const reference and another with const bool, which g++ didn't like). Let's see how this works.

Thanks, Lerner.

So the problem is that vector<bool> is specialized. When I overload operator [], I cannot return a bool reference simply because there is none; the compiler uses std::_Bit_reference. Using const bool reference also doesn't work because now the returned reference is that of a temporary (compiler warning).

I guess I have no choice but to use read()/write() interface functions.

This question has already been answered. Start a new discussion instead.