Overloading [] operator for a vector

This is a discussion on Overloading [] operator for a vector within the C++ Programming forums, part of the General Programming Boards category; Here is a snippet of code; I want to overload the [] operator such that, when I try to do ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    11

    Overloading [] operator for a vector

    Here is a snippet of code;
    I want to overload the [] operator such that, when I try to do something like
    vCont[i]->name
    it should check the mask against Contmask and take specific action.

    For example:

    vCont contents are

    vCont[0]->mask = 0x1;
    vCont[1]->mask = 0x2;
    vCont[2]->mask = 0x2;

    If Contmask = 0x2, then only vCont[1] and vCont[2] have to be accessed.

    So, is it possible to overload the [] operator so that it prints out an error message.


    Code:
    class Base
    {
       unsigned int mask;
       string name;
    };
    
    class Cont
    {
        unsigned int contmask;
        vector<Base *> vCont;
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,775
    I am not too clear on what you are trying to do, but if you really want to overload operator[] for std::vector, then you are out of luck: you cannot even modify std::vector in the first place.

    If you want to overload operator[] for your own container class (Cont?) that is implemented with std::vector, then yes, you can overload operator[]. However, in view of the convention in std::vector, I would suggest that this error checking based on a mask be done by throwing an exception from an at() member function.

    Furthermore, if by "take specific action" you mean to perform some other action than just returning a value, then operator[] and possibly at() are both inappropriate. Write a (member?) function with a more specific name instead.
    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

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    [] i think cannot be overloaded, you could overload the () operator, and access elements in () style instead of []
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,775
    [] i think cannot be overloaded, you could overload the () operator, and access elements in () style instead of []
    operator[] can indeed be overloaded.
    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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For example:

    vCont contents are

    vCont[0]->mask = 0x1;
    vCont[1]->mask = 0x2;
    vCont[2]->mask = 0x2;

    If Contmask = 0x2, then only vCont[1] and vCont[2] have to be accessed.
    Code:
    if (vCont[n]->mask = goodMask) {
        //access vCont[n]
    }
    else {
        //don't
    }
    What's the problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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