Thread: how to create a good operator[]

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    how to create a good operator[]

    Hi all,
    Well this is my problem.
    I am implementing a class that should act like an array of data.
    meaning that it should have a operator[].
    the problem is that my class may not hold the values in the array.
    like a bitArray class for example. (when needed it actually constracts the bit requested)
    I want to be able to write the code:

    bitArray &ba = new bitArray(100);
    ba[7] = 1;

    or
    in the case of huffman code and i want to get the 9'th byte in the compressed data

    HuffmanCode &hc = new HuffmanCode(File *src);
    x = hc[6];
    and so on.

    So what should operator[] return?
    I can't return some class Element since Element will need to know about the bitArray
    and bitArray will need to know about Element
    making it impossible to compile.
    what should i do?

    how do i implement it the way that i can use the sample code above.

    thanks a lot for any help.
    Last edited by mikel123; 01-10-2008 at 10:50 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe you might be able to return a proxy object (or reference to a proxy object) to handle this situation.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You could copy the idea of std::bitset. Provide a const version of operator[] that returns a bool. For the non-const version, return a reference of a proxy type that allows the caller to modify the bit via the proxy (so one does not actually modify the bit directly, but by modifying the value of the data object). The proxy type could be a nested class of your data class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    thank you daved and laserlight,
    your answers lead me to another question.
    can i overight the proxy class or it's functions? (which i think will be nested as laserlight said)
    If some class inherits my class.

    I ask this question since once i inherit my class the proxy class will need to be changed.

    thanks.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Interesting question. I'm sure you can set up the proxy class so that it's functionality remains appropriate for derived classes. Perhaps it has a reference to the parent class and then calls a virtual function to handle the assignment. Derived classes can override that virtual function to handle the assignment more appropriately.

    That's just my initial thought. Some might prefer a non-virtual function be called in the base class and the base class calls a virtual function for its implementation, but I'm not sure if that would be necessary for a proxy class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good Programming schools are they out there???
    By jbarby in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-12-2009, 06:31 PM
  2. Any good tutorials?
    By kimmag in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2008, 07:44 AM
  3. What are some good books on C?
    By php111 in forum C Programming
    Replies: 9
    Last Post: 10-01-2008, 06:16 AM
  4. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  5. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM