how to create a good operator[]

This is a discussion on how to create a good operator[] within the C++ Programming forums, part of the General Programming Boards category; Hi all, Well this is my problem. I am implementing a class that should act like an array of data. ...

  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 09:50 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    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
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,338
    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, 06: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, 11:15 AM
  5. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21