Re: How to use reference to do cast operator?
Immortal Nephi <Immortal_Nephi@hotmail.com> writes:
<snip />
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.
Having seen your attempt at writing your op[] in a subsequent post I can
agree with a lot of the comments made by Francesco in relation to it.
Since you have mentioned the possibility of using a proxy class (and
since we have looked at these together before also) I have quickly
thrown together an example (very likely not ideal) to illustrate how
this might be done. Since your classes store what are effectively three
rows of four elements, I have implemented the proxy to manage `columns'
of three elements via a tr1::tuple. This may not be viable nor what you
want but, as I have said, this is just an example.
I present my example by making additions to the code you have already
given (below).
/***** CODE *****/
#include <tr1/tuple>
template<typename T>
class Column_Proxy {
template<typename U>
friend class Column_Proxy;
public:
typedef std::tr1::tuple< T&, T&, T& > column_t;
Column_Proxy( T& first, T& second, T& third )
: column( first, second, third )
{ }
template<typename U>
Column_Proxy& operator=( const Column_Proxy<U>& cp ) {
std::tr1::get< 0 >( column ) = std::tr1::get< 0 >( cp.column );
std::tr1::get< 1 >( column ) = std::tr1::get< 1 >( cp.column );
std::tr1::get< 2 >( column ) = std::tr1::get< 2 >( cp.column );
return *this;
}
operator column_t( ) { return column; }
operator column_t( ) const { return column; }
private:
column_t column;
};
// class Class_A; forward declaration of Class_A not needed
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 ();
Column_Proxy< unsigned char > operator[ ] ( int i );
Column_Proxy< const unsigned char > operator[ ] ( int i ) const;
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 ();
Column_Proxy< unsigned char > operator[ ] ( int i );
Column_Proxy< const unsigned char > operator[ ] ( int i ) const;
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::operator 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;
}
Column_Proxy< unsigned char >
Class_A::operator[ ] ( int i ) {
return Column_Proxy< unsigned char >( m_Data_1[ i ],
m_Data_2[ i ],
m_Data_3[ i ]);
}
Column_Proxy< const unsigned char >
Class_A::operator[ ] ( int i ) const {
return Column_Proxy< const unsigned char >( m_Data_1[ i ],
m_Data_2[ i ],
m_Data_3[ i ]);
}
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::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;
}
Column_Proxy< unsigned char >
Class_B::operator[ ] ( int i ) {
return Column_Proxy< unsigned char >( m_Data_1[ i ],
m_Data_2[ i ],
m_Data_3[ i ] );
}
Column_Proxy< const unsigned char >
Class_B::operator[ ] ( int i ) const {
return Column_Proxy< const unsigned char >( m_Data_1[ i ],
m_Data_2[ i ],
m_Data_3[ i ] );
}
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;
}
/***** END CODE *****/
Just an idea.
Regards
Paul Bibbings