I
Immortal Nephi
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.
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:perator 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.
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 can examine complete code below if you wish.
class Class_A;
class Class_B;
class Class_A {
public:
Class_A();
Class_A( const Class_A &class_a );
~Class_A();
Class_A &Set_1( int index, unsigned char value );
Class_A &Set_2( int index, unsigned char value );
Class_A &Set_3( int index, unsigned char value );
unsigned char Get_1( int index );
unsigned char Get_2( int index );
unsigned char Get_3( int index );
operator Class_B ();
private:
unsigned char m_Data_1[ 4 ];
unsigned char m_Data_2[ 4 ];
unsigned char m_Data_3[ 4 ];
};
class Class_B {
public:
Class_B();
Class_B( const Class_B &class_b );
~Class_B();
Class_B &Set_1( int index, unsigned char value );
Class_B &Set_2( int index, unsigned char value );
Class_B &Set_3( int index, unsigned char value );
unsigned char Get_1( int index );
unsigned char Get_2( int index );
unsigned char Get_3( int index );
operator Class_A ();
private:
unsigned char m_Data_1[ 4 ];
unsigned char m_Data_2[ 4 ];
unsigned char m_Data_3[ 4 ];
};
Class_A::Class_A() {
for( int x = 0; x < 4; x++ )
m_Data_1[ x ] = m_Data_2[ x ] = m_Data_3[ x ] = 0;
}
Class_A::Class_A( const Class_A &class_a ) {
for( int x = 0; x < 4; x++ ) {
m_Data_1[ x ] = class_a.m_Data_1[ x ];
m_Data_2[ x ] = class_a.m_Data_2[ x ];
m_Data_3[ x ] = class_a.m_Data_3[ x ];
}
}
Class_A::~Class_A() {
}
Class_A &Class_A::Set_1( int index, unsigned char value ) {
m_Data_1[ index ] = value;
return *this;
}
Class_A &Class_A::Set_2( int index, unsigned char value ) {
m_Data_2[ index ] = value;
return *this;
}
Class_A &Class_A::Set_3( int index, unsigned char value ) {
m_Data_3[ index ] = value;
return *this;
}
unsigned char Class_A::Get_1( int index ) {
return m_Data_1[ index ];
}
unsigned char Class_A::Get_2( int index ) {
return m_Data_2[ index ];
}
unsigned char Class_A::Get_3( int index ) {
return m_Data_3[ index ];
}
Class_A:perator Class_B () {
Class_B b;
for( int x = 0; x < 4; x++ )
b.Set_1( x, m_Data_1[ x ] ).Set_2( x, m_Data_2[ x ] ).Set_3( x,
m_Data_3[ x ] );
return b;
}
Class_B::Class_B() {
for( int x = 0; x < 4; x++ )
m_Data_1[ x ] = m_Data_2[ x ] = m_Data_3[ x ] = 0;
}
Class_B::Class_B( const Class_B &class_b ) {
for( int x = 0; x < 4; x++ ) {
m_Data_1[ x ] = class_b.m_Data_1[ x ];
m_Data_2[ x ] = class_b.m_Data_2[ x ];
m_Data_3[ x ] = class_b.m_Data_3[ x ];
}
}
Class_B::~Class_B() {
}
Class_B &Class_B::Set_1( int index, unsigned char value ) {
m_Data_1[ index ] = value;
return *this;
}
Class_B &Class_B::Set_2( int index, unsigned char value ) {
m_Data_2[ index ] = value;
return *this;
}
Class_B &Class_B::Set_3( int index, unsigned char value ) {
m_Data_3[ index ] = value;
return *this;
}
unsigned char Class_B::Get_1( int index ) {
return m_Data_1[ index ];
}
unsigned char Class_B::Get_2( int index ) {
return m_Data_2[ index ];
}
unsigned char Class_B::Get_3( int index ) {
return m_Data_3[ index ];
}
Class_B:perator 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;
}
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 );
// First method
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 ) );
// Second method
a = b;
// Third method
for( int x = 0; x < 4; x++ )
a[ x ] = b[ x ];
return 0;
}
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.
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:perator 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.
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 can examine complete code below if you wish.
class Class_A;
class Class_B;
class Class_A {
public:
Class_A();
Class_A( const Class_A &class_a );
~Class_A();
Class_A &Set_1( int index, unsigned char value );
Class_A &Set_2( int index, unsigned char value );
Class_A &Set_3( int index, unsigned char value );
unsigned char Get_1( int index );
unsigned char Get_2( int index );
unsigned char Get_3( int index );
operator Class_B ();
private:
unsigned char m_Data_1[ 4 ];
unsigned char m_Data_2[ 4 ];
unsigned char m_Data_3[ 4 ];
};
class Class_B {
public:
Class_B();
Class_B( const Class_B &class_b );
~Class_B();
Class_B &Set_1( int index, unsigned char value );
Class_B &Set_2( int index, unsigned char value );
Class_B &Set_3( int index, unsigned char value );
unsigned char Get_1( int index );
unsigned char Get_2( int index );
unsigned char Get_3( int index );
operator Class_A ();
private:
unsigned char m_Data_1[ 4 ];
unsigned char m_Data_2[ 4 ];
unsigned char m_Data_3[ 4 ];
};
Class_A::Class_A() {
for( int x = 0; x < 4; x++ )
m_Data_1[ x ] = m_Data_2[ x ] = m_Data_3[ x ] = 0;
}
Class_A::Class_A( const Class_A &class_a ) {
for( int x = 0; x < 4; x++ ) {
m_Data_1[ x ] = class_a.m_Data_1[ x ];
m_Data_2[ x ] = class_a.m_Data_2[ x ];
m_Data_3[ x ] = class_a.m_Data_3[ x ];
}
}
Class_A::~Class_A() {
}
Class_A &Class_A::Set_1( int index, unsigned char value ) {
m_Data_1[ index ] = value;
return *this;
}
Class_A &Class_A::Set_2( int index, unsigned char value ) {
m_Data_2[ index ] = value;
return *this;
}
Class_A &Class_A::Set_3( int index, unsigned char value ) {
m_Data_3[ index ] = value;
return *this;
}
unsigned char Class_A::Get_1( int index ) {
return m_Data_1[ index ];
}
unsigned char Class_A::Get_2( int index ) {
return m_Data_2[ index ];
}
unsigned char Class_A::Get_3( int index ) {
return m_Data_3[ index ];
}
Class_A:perator Class_B () {
Class_B b;
for( int x = 0; x < 4; x++ )
b.Set_1( x, m_Data_1[ x ] ).Set_2( x, m_Data_2[ x ] ).Set_3( x,
m_Data_3[ x ] );
return b;
}
Class_B::Class_B() {
for( int x = 0; x < 4; x++ )
m_Data_1[ x ] = m_Data_2[ x ] = m_Data_3[ x ] = 0;
}
Class_B::Class_B( const Class_B &class_b ) {
for( int x = 0; x < 4; x++ ) {
m_Data_1[ x ] = class_b.m_Data_1[ x ];
m_Data_2[ x ] = class_b.m_Data_2[ x ];
m_Data_3[ x ] = class_b.m_Data_3[ x ];
}
}
Class_B::~Class_B() {
}
Class_B &Class_B::Set_1( int index, unsigned char value ) {
m_Data_1[ index ] = value;
return *this;
}
Class_B &Class_B::Set_2( int index, unsigned char value ) {
m_Data_2[ index ] = value;
return *this;
}
Class_B &Class_B::Set_3( int index, unsigned char value ) {
m_Data_3[ index ] = value;
return *this;
}
unsigned char Class_B::Get_1( int index ) {
return m_Data_1[ index ];
}
unsigned char Class_B::Get_2( int index ) {
return m_Data_2[ index ];
}
unsigned char Class_B::Get_3( int index ) {
return m_Data_3[ index ];
}
Class_B:perator 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;
}
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 );
// First method
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 ) );
// Second method
a = b;
// Third method
for( int x = 0; x < 4; x++ )
a[ x ] = b[ x ];
return 0;
}