954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

<Non-trivial> overloading operator []

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.

C++NOOOB
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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 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.

C++NOOOB
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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 []?

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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.

C++NOOOB
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

So the problem is that vector 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.

C++NOOOB
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

I seem to recall something about using a proxy , but I don't remember enough of it to do much more than mention it and some quick & dirty search .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You