|
From: Francesco <xtr...@vi...> - 2005-04-21 09:54:49
|
Thanks for answering Alfred,
I'm already using the STL but since I'm still in a learning stage I was=20=
trying to understand the "inner workings". Suppose I'm building ( just=20=
for my own pleasure :-) ) a simple competitor to the vector template.=20=
Or to make it even more banal, suppose I have the following code:
class MyData
{
typeHugeStuff * mHugeStuffPtr;
// ....
public:
// ....
MyData const & operator=3D( MyData const & inMyData ); // the =
definition=20
of this copies also the stuff pointed to by mHugeStuffPtr
// .....
};
template< typename T >
void MySwap( T * inDataPtr1, T * inDataPtr2 )
{
#ifdef BAD_SWAP
// the following will swap in memory the two objects but will =
also=20
deallocate/allocate/copy hugeStuff
T tempData;
tempData =3D *inDataPtr1;
*inDataPtr1 =3D *inDataPtr2;
*inDataPtr2 =3D tempData;
#else
// the following will just swap bitwise the members of the two =
objects=20
without erasing/duplicating/whatever hugeStuff
// but this bitwise copy seems to be slower than using the =
operator=3D=20
(when it's not overloaded)
// so I was wondering if it is possible to access the =
UNoverloaded=20
operator=3D in objects that have overloaded it....
char * aPtr;
aPtr =3D (char*) malloc ( sizeof( T ) );
memcpy( aPtr, inDataPtr1, sizeof( T ) );
memcpy( inDataPtr1, inDataPtr2, sizeof( T ) );
memcpy( inDataPtr2, aPtr, sizeof( T ) );
free( aPtr );
#endif
}
void main( void )
{
MyData * aDataPtr;
=09
aDataPtr =3D new MyData[ 100 ];
FillUp( aDataPtr, 100 ); // filling up with data
MySwap< MyData >( aDataPtr + 20, aDataPtr + 40 ); // just =
because.... =20
:-)
}
Thanks in advance for any insight,
Francesco
On Apr 20, 2005, at 7:46 PM, Alfred P. Reaud wrote:
> Hi Francesco,
> =A0
> Your array of class objects would be better served if it where a list=20=
> or vector of class objects instead of an array.=A0 Then you can use =
the=20
> iterator features, including list and vector specific swap and sort=20
> member functions.=A0 I f you must use an array of class objects, =
then=20
> there's always the <algorithm> standard template library header.=A0=20
> Algorithm has a rich set of functions that include swap, sort, and=20
> other useful data manipulation algorithms such as unique, etc.=A0 =
These=20
> functions are usually optimized, and work in most instances=20
> significantly better than manually coded swap or sort functions.
> =A0
> I'm not actually sure what you mean by "deep copy", however, I'm=20
> assuming that you mean actual data copying.=A0 You shouldn't be =
copying=20
> data to swap it!=A0 You should be swapping pointers to the data.=A0 =
Maybe=20
> the issue is in using a class class_objects[array_size]; declaration=20=
> vs. a class *class_objects; declaration, because the overloaded=20
> operator=3D may operate in a different fashion between a pointer and =
an=20
> array element (i.e. class_objects[7], is a pointer, but in machine=20
> code is actually a set of pointer arithmetic operations).=A0 The class=20=
> operator=3D should already be configured to correctly do something =
like:
> =A0
> class *obj_a, *obj_b, *temp;
> obj_a =3D new class; ...;
> temp =3D obj_a; obj_a =3D obj_b; obj_b =3D temp;
> delete(temp); ...
> =A0
> so that should work correctly, ***** however *****
> =A0
>
> list<class> obj_list;
> list<class>::iterator obj_iter;
> obj_list.push_back(obj_a); ...
> obj_iter =3D obj_list.begin();
> obj_list.sort() or obj_list.sort( compare_function );
> ...
> =A0
> is easier, more elegant, and uses the optimized features of C++!=A0=20
> Hopefully the above helps!
>
> Francesco <xtr...@vi...> wrote:
> Hi everybody,
> I'm a total beginner and I have the following problem:
> I have an array of objects instantiated from a class that has member
> pointers to dynamically allocated data. This class has also the usual
> overloaded operator=3D. I have to sort this array and I need to do =
some
> swapping. But swapping by "deep copy" seems useless to me in this=20
> case,
> thus is it possible somehow to override the overloaded operator=3D =
and
> use just for the swapping the normal operator =3D? I could use memcpy=20=
> for
> a bitwise copy, but it seems to be significantly slower, especially
> for small objects.
> Thanks in advance,
> Francesco
> <snip>
>
>
>
> Alfred P. Reaud
> <image.tiff>
|