Pointer arithmetic

D

dan

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
 
J

Jeff Schwab

dan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top