Umm, dereferencing pointers to a class...I think
Ok....
class SomeOtherClass;
class MyClass
{
SomeOtherClass* pBlah[SomeArrayNumber];
}
A few lines of code later..........
MyClass* pClass;
SomeOtherClass TempClass;
TempClass = *pClass->pBlah[SomeNumber];
Ok now my question is when I modify TempClass, why does pClass->pBlah get modified to? Didn''t I just assign a copy of it to TempClass...not a pointer to the original.
May 25, 2000 02:29 PM
Sorry, but you just dereferenced a pointer to myclass,
you should try myclass->(*someother).
you should try myclass->(*someother).
I figured out the answer to my own problem a couple minutes after I posted it, but I created some new ones
class MyClass
{
D3DVERTEX* Vertices;
DWORD NumVertices;
}
....
MyClass* pSomeClass;
pSomeClass->Vertices = new D3DVERTEX[pSomeClass->NumVertices];
....
DWORD NumVertices = pSomeClass->NumVertices;
D3DVERTEX Vertices[NumVertices];
Ok how can I create an array like that? New doesn't seem to work because its not a pointer.
And once I do create it....
Vertices = *pSomeClass->Vertices;
Will that copy the whole array? Or just the first member or something.
Edited by - jLeslie on May 25, 2000 3:37:34 PM
class MyClass
{
D3DVERTEX* Vertices;
DWORD NumVertices;
}
....
MyClass* pSomeClass;
pSomeClass->Vertices = new D3DVERTEX[pSomeClass->NumVertices];
....
DWORD NumVertices = pSomeClass->NumVertices;
D3DVERTEX Vertices[NumVertices];
Ok how can I create an array like that? New doesn't seem to work because its not a pointer.
And once I do create it....
Vertices = *pSomeClass->Vertices;
Will that copy the whole array? Or just the first member or something.
Edited by - jLeslie on May 25, 2000 3:37:34 PM
Here''s a complete example of a resizable array, in particular, of TLVertices.
With a few simple tweaks this could be a template class ( in fact, maybe I will end up making it a template class )
It is just a simple resizable block array with an explicit conversion method for Direct3D ADI compatibility.
Hopefully it isn''t too long and the formatting comes out ok
class DXIMPEXP DXTLVertexArray
{
private:
////////////////////
//
// a raw pointer to an array of vertices
D3DTLVERTEX *iVertices;
////////////////////
//
// the number of vertices
long iNumVertices;
public:
DXTLVertexArray();
DXTLVertexArray(long initialSize);
DXTLVertexArray(const DXTLVertexArray& copy);
virtual ~DXTLVertexArray();
DXTLVertexArray& operator=(const DXTLVertexArray& rhs);
void Resize(long newSize);
long GetSize(void) const;
// a compatibility explicit cast operator
// for using this with D3D API
D3DTLVERTEX* AsVerticesArray(void) const;
// a compatibility explicit cast operator
// for using this with D3D API
// this allows grabbing another ''section'' of
// the vertices array, specifically for doing
// triangle strips
D3DTLVERTEX* AsIndexedVerticesArray(long index) const;
D3DTLVERTEX& operator[](long index);
const D3DTLVERTEX& operator[](long index) const;
};
DXTLVertexArray::DXTLVertexArray()
: iVertices( 0 ),
iNumVertices( 0 )
{
}
DXTLVertexArray::DXTLVertexArray(long initialSize)
: iVertices( 0 ),
iNumVertices( initialSize )
{
iVertices = new D3DTLVERTEX[iNumVertices];
memset( iVertices, 0, sizeof(D3DTLVERTEX) * iNumVertices );
}
DXTLVertexArray::DXTLVertexArray(const DXTLVertexArray& copy)
: iVertices( 0 ),
iNumVertices( 0 )
{
if ( copy.iNumVertices > 0 )
{
iVertices = new D3DTLVERTEX[copy.iNumVertices];
iNumVertices = copy.iNumVertices;
memcpy( iVertices, copy.iVertices, sizeof(D3DTLVERTEX) * iNumVertices );
}
}
DXTLVertexArray::~DXTLVertexArray()
{
delete iVertices;
iVertices = 0;
iNumVertices = 0;
}
DXTLVertexArray& DXTLVertexArray::operator=(const DXTLVertexArray& rhs)
{
if ( this != &rhs )
{
delete iVertices;
iVertices = 0;
iNumVertices = 0;
if ( rhs.iNumVertices > 0 )
{
iVertices = new D3DTLVERTEX[rhs.iNumVertices];
iNumVertices = rhs.iNumVertices;
memcpy( iVertices, rhs.iVertices, sizeof(D3DTLVERTEX) * iNumVertices );
}
}
return *this;
}
void DXTLVertexArray::Resize(long newSize)
{
if ( newSize == iNumVertices )
return;
D3DTLVERTEX *tmp = 0;
try
{
if ( newSize > iNumVertices )
{
// want bigger
tmp = new D3DTLVERTEX[newSize];
// copy that old stuff into the new one
memcpy( tmp, iVertices, sizeof(D3DTLVERTEX) * iNumVertices );
// delete the old one
delete iVertices;
// set in the new one
iVertices = tmp;
tmp = 0;
iNumVertices = newSize;
}
else
{
// want smaller
tmp = new D3DTLVERTEX[newSize];
memcpy( tmp, iVertices, sizeof(D3DTLVERTEX) * newSize );
// delete the old one
delete iVertices;
// set in the new one
iVertices = tmp;
tmp = 0;
iNumVertices = newSize;
}
}
catch(...)
{
// make sure we delete tmp if it has not been properly handled
delete tmp;
// rethrow
throw;
}
}
long DXTLVertexArray::GetSize(void) const
{
return iNumVertices;
}
D3DTLVERTEX* DXTLVertexArray::AsVerticesArray(void) const
{
return const_cast( iVertices );
}
D3DTLVERTEX* DXTLVertexArray::AsIndexedVerticesArray(long index) const
{
if ( index < 0 // index > iNumVertices )
throw DXValOutOfRange( index, 0, iNumVertices-1, __FILE__, __LINE__ );
return const_cast( &(iVertices[index]) );
}
D3DTLVERTEX& DXTLVertexArray::operator[](long index)
{
if ( index < 0 // index > iNumVertices )
throw DXValOutOfRange( index, 0, iNumVertices-1, __FILE__, __LINE__ );
return iVertices[index];
}
const D3DTLVERTEX& DXTLVertexArray::operator[](long index) const
{
if ( index < 0 // index > iNumVertices )
throw DXValOutOfRange( index, 0, iNumVertices-1, __FILE__, __LINE__ );
return iVertices[index];
}
// example usage:
DXTLVertexArray vertices(4);
// do something to create the vertices
SOME_D3D_METHOD_THAT_TAKES_VERTEX_PTR( vertices.AsVerticesArray(), vertices.GetSize() );
With a few simple tweaks this could be a template class ( in fact, maybe I will end up making it a template class )
It is just a simple resizable block array with an explicit conversion method for Direct3D ADI compatibility.
Hopefully it isn''t too long and the formatting comes out ok
class DXIMPEXP DXTLVertexArray
{
private:
////////////////////
//
// a raw pointer to an array of vertices
D3DTLVERTEX *iVertices;
////////////////////
//
// the number of vertices
long iNumVertices;
public:
DXTLVertexArray();
DXTLVertexArray(long initialSize);
DXTLVertexArray(const DXTLVertexArray& copy);
virtual ~DXTLVertexArray();
DXTLVertexArray& operator=(const DXTLVertexArray& rhs);
void Resize(long newSize);
long GetSize(void) const;
// a compatibility explicit cast operator
// for using this with D3D API
D3DTLVERTEX* AsVerticesArray(void) const;
// a compatibility explicit cast operator
// for using this with D3D API
// this allows grabbing another ''section'' of
// the vertices array, specifically for doing
// triangle strips
D3DTLVERTEX* AsIndexedVerticesArray(long index) const;
D3DTLVERTEX& operator[](long index);
const D3DTLVERTEX& operator[](long index) const;
};
DXTLVertexArray::DXTLVertexArray()
: iVertices( 0 ),
iNumVertices( 0 )
{
}
DXTLVertexArray::DXTLVertexArray(long initialSize)
: iVertices( 0 ),
iNumVertices( initialSize )
{
iVertices = new D3DTLVERTEX[iNumVertices];
memset( iVertices, 0, sizeof(D3DTLVERTEX) * iNumVertices );
}
DXTLVertexArray::DXTLVertexArray(const DXTLVertexArray& copy)
: iVertices( 0 ),
iNumVertices( 0 )
{
if ( copy.iNumVertices > 0 )
{
iVertices = new D3DTLVERTEX[copy.iNumVertices];
iNumVertices = copy.iNumVertices;
memcpy( iVertices, copy.iVertices, sizeof(D3DTLVERTEX) * iNumVertices );
}
}
DXTLVertexArray::~DXTLVertexArray()
{
delete iVertices;
iVertices = 0;
iNumVertices = 0;
}
DXTLVertexArray& DXTLVertexArray::operator=(const DXTLVertexArray& rhs)
{
if ( this != &rhs )
{
delete iVertices;
iVertices = 0;
iNumVertices = 0;
if ( rhs.iNumVertices > 0 )
{
iVertices = new D3DTLVERTEX[rhs.iNumVertices];
iNumVertices = rhs.iNumVertices;
memcpy( iVertices, rhs.iVertices, sizeof(D3DTLVERTEX) * iNumVertices );
}
}
return *this;
}
void DXTLVertexArray::Resize(long newSize)
{
if ( newSize == iNumVertices )
return;
D3DTLVERTEX *tmp = 0;
try
{
if ( newSize > iNumVertices )
{
// want bigger
tmp = new D3DTLVERTEX[newSize];
// copy that old stuff into the new one
memcpy( tmp, iVertices, sizeof(D3DTLVERTEX) * iNumVertices );
// delete the old one
delete iVertices;
// set in the new one
iVertices = tmp;
tmp = 0;
iNumVertices = newSize;
}
else
{
// want smaller
tmp = new D3DTLVERTEX[newSize];
memcpy( tmp, iVertices, sizeof(D3DTLVERTEX) * newSize );
// delete the old one
delete iVertices;
// set in the new one
iVertices = tmp;
tmp = 0;
iNumVertices = newSize;
}
}
catch(...)
{
// make sure we delete tmp if it has not been properly handled
delete tmp;
// rethrow
throw;
}
}
long DXTLVertexArray::GetSize(void) const
{
return iNumVertices;
}
D3DTLVERTEX* DXTLVertexArray::AsVerticesArray(void) const
{
return const_cast( iVertices );
}
D3DTLVERTEX* DXTLVertexArray::AsIndexedVerticesArray(long index) const
{
if ( index < 0 // index > iNumVertices )
throw DXValOutOfRange( index, 0, iNumVertices-1, __FILE__, __LINE__ );
return const_cast( &(iVertices[index]) );
}
D3DTLVERTEX& DXTLVertexArray::operator[](long index)
{
if ( index < 0 // index > iNumVertices )
throw DXValOutOfRange( index, 0, iNumVertices-1, __FILE__, __LINE__ );
return iVertices[index];
}
const D3DTLVERTEX& DXTLVertexArray::operator[](long index) const
{
if ( index < 0 // index > iNumVertices )
throw DXValOutOfRange( index, 0, iNumVertices-1, __FILE__, __LINE__ );
return iVertices[index];
}
// example usage:
DXTLVertexArray vertices(4);
// do something to create the vertices
SOME_D3D_METHOD_THAT_TAKES_VERTEX_PTR( vertices.AsVerticesArray(), vertices.GetSize() );
quote: Original post by jLeslie
Ok....
class SomeOtherClass;
class MyClass
{
SomeOtherClass* pBlah[SomeArrayNumber];
}
A few lines of code later..........
MyClass* pClass;
SomeOtherClass TempClass;
TempClass = *pClass->pBlah[SomeNumber];
Ok now my question is when I modify TempClass, why does pClass->pBlah get modified to? Didn''t I just assign a copy of it to TempClass...not a pointer to the original.
pClass is an invalid pointer, since you didn''t allocate any memory for an object of that type. Therefore the effects of doing things to it are most likely undefined. How can you know that pClass->pBlah[SomeNumber] has changed when you never set it to anything in the first place? You also don''t say anything about allocating memory for the objects pointed to by pBlah[0], pBlah[1], etc.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement