Re: How to use reference to do cast operator?
Immortal Nephi <Immortal_Nephi@hotmail.com>, on 18/07/2010 18:20:18, wrote:
I write two different classes. Two classes have their own memory
that holds data in array. They can have different algorithms to
manipulate data.
I would like to write my code to transfer data between two classes.
First class has tasks to process data before it can transfer data to
second class. After the data transfer is complete, both classes use
their own algorithms to manipulate data.
I see two steps here above:
- process the data in the first class (and I would do that either in the
construction step or in the first call to the getter function)
- transfer the data to the second class (and I would do it with an
assignment operator that takes a const reference to the first class)
Implementing the second step as an assignment operator has the advantage
of not having to worry about temporaries, assigning directly to the
private data of the class that is being assigned to.
My code looks like that:
int main() {
Class_A a;
Class_B b;
a.Set_1( 0, 0x41 ).Set_2( 0, 0x42 ).Set_3( 0, 0x43 );
a.Set_1( 1, 0x44 ).Set_2( 1, 0x45 ).Set_3( 1, 0x46 );
a.Set_1( 2, 0x47 ).Set_2( 2, 0x48 ).Set_3( 2, 0x49 );
a.Set_1( 3, 0x4a ).Set_2( 3, 0x4b ).Set_3( 3, 0x4c );
b.Set_1( 0, 0x71 ).Set_2( 0, 0x72 ).Set_3( 0, 0x73 );
b.Set_1( 1, 0x74 ).Set_2( 1, 0x75 ).Set_3( 1, 0x76 );
b.Set_1( 2, 0x77 ).Set_2( 2, 0x78 ).Set_3( 2, 0x79 );
b.Set_1( 3, 0x7a ).Set_2( 3, 0x7b ).Set_3( 3, 0x7c );
for( int x = 0; x< 4; x++ )
a.Set_1( x, b.Get_1( x ) ).Set_2( x, b.Get_2( x ) ).Set_3( x,
b.Get_3( x ) );
return 0;
}
Do you notice for loop? For loop transfers data from class b to
class a. I decide to replace from for loop to cast operator. You can
write that code like this.
// for( int x = 0; x< 4; x++ )
// a.Set_1( x, b.Get_1( x ) ).Set_2( x, b.Get_2( x ) ).Set_3( x,
b.Get_3( x ) );
a = b; // class b is converted to class a by moving data
The cast operator function looks like:
Class_B::operator Class_A () {
Class_A a;
for( int x = 0; x< 4; x++ )
a.Set_1( x, m_Data_1[ x ] ).Set_2( x, m_Data_2[ x ] ).Set_3( x,
m_Data_3[ x ] );
return a;
}
It is possible that transferring data between memory can cause
overhead because cast operator creates temporary class a in memory
before data transfer begins. After cast operator function terminates,
temporary class a is created second time because it uses copy
constructor.
Temporaries may be optimized out by the compiler, depending on the
cases. I don't think you really need a conversion operator (the one you
call "cast" operator) - the assignment operator I mentioned above should
be more efficient - unless, maybe, if the class needs special care in
case of self-assignment, which seems not to be your case.
After copy constructor is completed, temporary class a is
deallocated. Return to the main() function, temporary class a for
copy constructor is deallocated.
You can see that data transfer requires temporary class a twice. How
do you use reference? Reference can avoid reallocate memory twice.
Sometimes, I want to use operator[] to select element in the array in
class b and transfers it to class a rather than copying all data in
memory.
for( int x = 0; x< 4; x++ )
a[ x ] = b[ x ]; // use operator [] to do cast operator
// a = b; // All elements from class b is transferred to class a
without using operator[]
I can?t think how to add operator[] to both class a and class b.
Maybe, proxy class is needed. I tried to write operator[] function
but it did not work.
You didn't post your operator[], which was the problem? I don't think
there should be any problem in implementing it for both of your classes.
--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com