| | |
<Non-trivial> overloading operator []
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
Hi,
I have class which has two bool vectors:
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.
I have class which has two bool vectors:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
I guess I have no choice but to use read()/write() interface functions.
0
#7 Oct 10th, 2009
"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
![]() |
Similar Threads
- polynomial: problem with overloading operator = (C++)
- Overloading of () operator (C++)
- overloading operator >> (C++)
- Overloading operator= (C++)
- problem with operator overloading (C++)
- overloading operator [] (C++)
- need help with simple overloading operator+ for adding two object data. (C++)
Other Threads in the C++ Forum
- Previous Thread: Urgent help #2
- Next Thread: Fibonacci Series
Views: 314 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






