<Non-trivial> overloading operator []

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 12
Reputation: C++NOOOB is an unknown quantity at this point 
Solved Threads: 0
C++NOOOB C++NOOOB is offline Offline
Newbie Poster

<Non-trivial> overloading operator []

 
0
  #1
Oct 9th, 2009
Hi,

I have class which has two bool vectors:
  1. typedef vector<bool> my_bool_vector;
  2. class MyClass{
  3. private:
  4. my_bool_vector bvec;
  5. my_bool_vector another_bvec;
  6. public:
  7. explicit MyClass(unsigned int size) :
  8. bvec(size, false),
  9. another_bvec(size, false) {
  10. }
  11. };
  12. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso
 
1
  #2
Oct 9th, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: C++NOOOB is an unknown quantity at this point 
Solved Threads: 0
C++NOOOB C++NOOOB is offline Offline
Newbie Poster
 
0
  #3
Oct 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #4
Oct 9th, 2009
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 []?
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: C++NOOOB is an unknown quantity at this point 
Solved Threads: 0
C++NOOOB C++NOOOB is offline Offline
Newbie Poster
 
0
  #5
Oct 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: C++NOOOB is an unknown quantity at this point 
Solved Threads: 0
C++NOOOB C++NOOOB is offline Offline
Newbie Poster
 
0
  #6
Oct 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #7
Oct 10th, 2009
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 314 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC