Thread: accesing a class's overloaded [] via a pointer

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Question accesing a class's overloaded [] via a pointer

    Hello,

    Hope someone can help me with this. I have searched the web with no luck.

    I have an array class that I create instances of dynamically. However when I attempt to use the overloaded operators using my class pointers I get errors and/or unexpected results.

    If I declare the object in the function (not use a passed pointer to the object declared elsewhere) it works fine, but the pointer does not.

    The pertinent code is (I hope it is clear.):

    Code:
    class RANGE_ARRAY
    {
    private:
         UINT*  _array;
         int _size;
    
    public:
         RANGE_ARRAY()
         {
              _array = new UINT[8];
              _size = 8;
         };
    
         UINT& operator [] (int index)
         {
               if (index >= 0 && index < _size)
                     return _array[index];
         };
    };
    
    
    void fakeUseRange(RANGE_ARRAY* ra)
    {
         int count = 0;
         while (count < 8)
         {
               ra[8] = 68;
          }
    
    return;
    }
    Last edited by mnj; 03-11-2004 at 08:56 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You have to dereference the pointer, ie:

    (*ra)[8] = 68

    Some other things I noticed:

    - you have semicolens following some of the functions. Typo?

    - in the function:

    Code:
    UINT& operator [] (int index)
         {
               if (index >= 0 && index < _size)
                     return _array[index];
         };
    ...if 'index' is out of range - what get's returned? You have to account for every possibility.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Ahaaa!!! Thank you Sebastiani!

    yes, typos. I omitted the else return, since I didn't think it was necesary to understand the problem at hand.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Ahaaa!!! Thank you Sebastiani!

    Originally posted by mnj
    yes, typos. I omitted the else return, since I didn't think it was necesary to understand the problem at hand.
    the thing is, you are making a check in code to see if the index is in range, but you aren't doing anything about it. the check might as well not be there if you aren't throwing an exception or making an assertion or anything. right now your function will just return garbage and the person using the function will be oblivious to his mistake.

    also, an alternative way of accessing the overloaded [] is by using it by name (but I wouldn't recommend it because of readability)

    ra->operator []( 8 ) = 68

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    re Polymorphic

    What I meant in my reply is that I ommited the error checking code from the sample I put on the forum, since there error checking part of the code wasn't what I was having touble with. Thanks, however, for you reply. I agree with you, that the alternitive way that you posted does favor illegibility a bit.

    MnJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM