944,030 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 575
  • C++ RSS
Oct 9th, 2009
0

<Non-trivial> overloading operator []

Expand Post »
Hi,

I have class which has two bool vectors:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++NOOOB is offline Offline
12 posts
since Sep 2009
Oct 9th, 2009
1
Re: <Non-trivial> overloading operator []
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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 9th, 2009
0
Re: <Non-trivial> overloading operator []
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++NOOOB is offline Offline
12 posts
since Sep 2009
Oct 9th, 2009
0
Re: <Non-trivial> overloading operator []
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 []?
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 9th, 2009
0
Re: <Non-trivial> overloading operator []
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++NOOOB is offline Offline
12 posts
since Sep 2009
Oct 10th, 2009
0
Re: <Non-trivial> overloading operator []
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++NOOOB is offline Offline
12 posts
since Sep 2009
Oct 10th, 2009
0
Re: <Non-trivial> overloading operator []
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Urgent help #2
Next Thread in C++ Forum Timeline: Compiler Errors, Warning, Differences Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC