Pointer arithmetic

Discussion in 'C++' started by dan, Jan 6, 2004.

  1. dan

    dan Guest

    Hello,
    An Iterator class declares a data member that points to the current
    index of an array within a container class. It also implements a
    member function that determines whether or not the data member is
    pointing to the last index in the array using pointer arithmetic.

    class CIterator
    {
    public:
    CIterator(CContainer& c) : myContainer,
    mypData(myContainer.mypArray) {}

    void Reset() { mypData = myContainer.mypArray; }
    bool IsEnd()
    {
    return ((mypData - myContainer.mypArray) >=
    myContainer.mySize);
    }

    private:
    CContainer& myContainer;
    int *mypData;
    };

    My question is in regards to the way c++ points to memory. If {
    mypData = myContainer.mypArray; } assigns the address of the first
    element in the array to mypData, then how is it that ((mypData -
    myContainer.mypArray) >= myContainer.mySize); the first operand which
    is basically an address in memory, (correct?) can relate to the second
    operand (mySize) which is an int with a relational operator? the 2
    data types don't seem compatible.

    Daniel
    dan, Jan 6, 2004
    #1
    1. Advertising

  2. dan

    Jeff Schwab Guest

    dan wrote:
    > Hello,
    > An Iterator class declares a data member that points to the current
    > index of an array within a container class. It also implements a
    > member function that determines whether or not the data member is
    > pointing to the last index in the array using pointer arithmetic.
    >
    > class CIterator
    > {
    > public:
    > CIterator(CContainer& c) : myContainer,
    > mypData(myContainer.mypArray) {}
    >
    > void Reset() { mypData = myContainer.mypArray; }
    > bool IsEnd()
    > {
    > return ((mypData - myContainer.mypArray) >=
    > myContainer.mySize);
    > }
    >
    > private:
    > CContainer& myContainer;
    > int *mypData;
    > };
    >
    > My question is in regards to the way c++ points to memory. If {
    > mypData = myContainer.mypArray; } assigns the address of the first
    > element in the array to mypData, then how is it that ((mypData -
    > myContainer.mypArray) >= myContainer.mySize); the first operand which
    > is basically an address in memory, (correct?)


    No, it's difference between two addresses. It's usually a signed
    integer type called std::ptrdiff_t (defined in <cstddef>).

    > can relate to the second
    > operand (mySize) which is an int with a relational operator? the 2
    > data types don't seem compatible.
    >
    > Daniel
    Jeff Schwab, Jan 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. a

    pointer arithmetic

    a, Sep 11, 2003, in forum: C++
    Replies:
    6
    Views:
    521
  2. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    845
    tom_usenet
    Dec 8, 2003
  3. ceo
    Replies:
    8
    Views:
    356
    Pete Becker
    Mar 10, 2005
  4. Replies:
    21
    Views:
    1,137
    Richard Herring
    Apr 27, 2005
  5. joshc
    Replies:
    5
    Views:
    556
    Keith Thompson
    Mar 31, 2005
Loading...

Share This Page