Operator overloading

This is a discussion on Operator overloading within the C++ Programming forums, part of the General Programming Boards category; Code: class MyClass { private: unsigned long len; unsigned long *data; public: // Constructor // Overloaded Operators MyClass & operator=(const ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    Operator overloading

    Code:
    class MyClass {
        private:
          unsigned long len;
          unsigned long *data;
    
        public:
          // Constructor
     
          // Overloaded Operators
         MyClass & operator=(const MyClass &);
          MyClass & operator[](unsigned long i);
          char * operator[](unsigned long i) const;
          bool operator==(const MyClass &other) const;
    
        
      };
    
    I am trying to implement the [] and == overloading. But am getting seg fault.
    
    char *MyClass:: operator[] (unsigned long i) const
    {
        return (data[i]);
    
    }
    
    bool MyClass: operator==(const MyClass &other) const
    {
        for ( i =0; i< len ; i++)
        {
            if ( this->operator[](i) == other.operator[] (i))
               set true;
            else
            { set false; break;}
        }
        return true or false;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to check for going over the array bounds (and, if it happens, what do you want to do about it).

    Also, I'm reeeeeeeally hoping that you don't have things like "set true" and "return true or false" in your actual code.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Do you return char * for operator[] because you are trying to access the bytes of a long? Currently, the code will treat data like an array of longs, and convert element i of said array to a char pointer.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    7
    @whiteflags - Yeah, I am trying to return bytes of the 'data' array in the [] overload. How do I do this?

    @tabstop: I have actual code to set boolean flags.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Since you want to return a bytestring I would do it like so: always access data normally to get the specific long you want. Use bit-wise operators to extract the bits. Those bits you can store in a more convenient object and return that. At the minimum, make an array on the heap of unsigned chars.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You haven't shown the constructor for this class. For all we know you might be failing to initialise data or allocating an insufficient amount of memory.
    Why is that last function a hybrid between actual code and pseudocode?

    If you want a proper diagnosis you have to post the real code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator Overloading....
    By darren78 in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2010, 10:01 AM
  2. Overloading operator++
    By vopo in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2008, 03:52 AM
  3. Operator Overloading
    By kennny2004 in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 12:07 AM
  4. Overloading new operator
    By gustavosserra in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2003, 06:13 PM
  5. Overloading operator
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 06-26-2002, 03:18 AM

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