|
-
August 24th, 2011, 10:51 AM
#1
Object not retaining modified values
I have a class (DataBuffer) that has functions that return objects of DataBuffer but when I'm calling other class functions from within the function that returns the object of DataBuffer, the internal values are not being set after the function returns.
So, basically (if you made it through that first sentence), I have a function of DataBuffer that is calling a different function within DataBuffer to set some values but when it returns, the values aren't the same.
To the code!
DataBuffer.h
Code:
class DataException;
class DataBuffer
{
public:
DataBuffer ();
DataBuffer (const DataBuffer &rhs);
DataBuffer (unsigned int width, unsigned int height, float fillValue=0.0)
throw (DataException);
void setDimensions (unsigned int width, unsigned int height, float fillValue=0.0)
throw (DataException);
DataBuffer func ();
protected:
float **allocate (unsigned int width, unsigned int height)
throw (DataException);
unsigned int width;
unsigned int height;
float **buffer;
}
DataBuffer.cpp
Code:
DataBuffer::DataBuffer ()
: width (0),
height (0),
buffer (NULL)
{
}
DataBuffer::DataBuffer (const DataBuffer &rhs)
: width (rhs.width),
height (rhs.height),
buffer (rhs.buffer)
{
}
DataBuffer::DataBuffer (unsigned int width, unsigned int height, float *fillValue)
throw (DataException)
{
try
{
buffer = allocate (width, height);
}
catch (DataException &e)
{
e.addToStack ("DataBuffer::DataBuffer (unsigned int, unsigned int, float);
throw e;
}
for (unsinged int i=0 ; i<height ; ++i)
{
for (unsigned int j=0 ; j<width ; ++j)
{
buffer[i][j] = fillValue;
}
}
}
void DataBuffer::setDimensions (unsigned int width, unsigned int height, float fillValue)
throw (DataException)
{
if ((width !=0) && (height != 0))
{
this->width = width;
this->height = height;
try
{
buffer = allocate (width, height);
}
catch (DataException &e)
{
e.addToStack ("DataBuffer::setDimensions()");
throw e;
}
for (unsigned int i=0 ; i<height ; ++i)
{
for (unsigned int j=0 ; j<width ; ++j)
{
buffer[i][j] = fillValue;
}
}
}
else
{
throw DataException ("One of the dimensions is set to zero (0)", "DataBuffer::setDimensions()");
}
}
float** DataBuffer::allocate (unsigned int width, unsigned int height)
throw (DataException)
{
float **result;
...
return result;
}
DataBuffer DataBuffer::func ()
{
DataBuffer result;
try
{
result.setDimensions (this->width, this->height);
}
catch (DataException &e)
{
e.addToStack ("DataBuffer::func()");
throw e;
}
...
return result;
}
My problem is that after calling setDimensions()in func()the values in result don't match what I passed in.
When I step into setDimensions() in the debugger (gdb) the values all get set properly but when it exits, the values in result are changed from what they were before setDimensions() but they aren't what was set in the function.
I can't see anything wrong with what I'm doing but there is obviously something incorrect that I am missing . I would be greatly appreciated if someone could help me figure out what I'm doing wrong.
Thanks.
BTW, this is using g++ (GCC) 4.4.5 on Fedora 13
Last edited by mazeing; August 24th, 2011 at 11:52 AM.
------------------------------------------------------
There are only 10 types of people in the world.
Those who understand binary and those that don't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|