An object returning part of itself
Is it valid for an object to return an object of they same type?
Please look at the getBlock method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
class memBlock
{
private:
char *mem;
unsigned long length;
public:
memBlock(char *data,unsigned long startPoint, unsigned long endPoint)
{
length = endPoint - startPoint;
mem = new char[endPoint - startPoint];
for(unsigned long index = startPoint; index < endPoint;index++)
mem[index] = data[index]; //copy the data
}
memBlock(char *data, unsigned long len)
{
length = len;
mem = new char[length];
for(unsigned long index = 0; index < length;index++)
mem[index] = data[index]; //copy the data
}
~memBlock()
{
delete mem;
}
int GetBit(unsigned long index)
{
unsigned long byteIndex = index/8;//get the byte to read
unsigned long bitIndex = index%8; //get the bit to read
if(byteIndex > length)
return -1;
else
return mem[byteIndex] & (1 << bitIndex);
};
void SetBit(unsigned long index)
{
unsigned long byteIndex = index/8;//get the byte to read
unsigned long bitIndex = index%8; //get the bit to read
if(!(byteIndex > length))
mem[byteIndex] |= 1 << bitIndex;
};
memBlock getBlock(unsigned long startingPoint,unsigned long endPoint)
{
return memBlock(mem,startingPoint,endPoint);
}
};
|
Topic archived. No new replies allowed.