Re: Temporary Class through Reference
On Monday, February 4, 2013 11:03:23 PM UTC-6, Nephi Immortal wrote:
I would like to demonstrate my code how Value class can use reference ins=
tead of value type. I may want to use helper class when two or more functi=
ons are connected together in one line such as F1().F2().F3()=85=85
I will put Value class in nested class later.
Unfortunately, I am unable to use standard operator+ and operator+= suc=
h as Value& operator+( Value const &left, Value const &right ). If I am go=
ing to use this function, then some local variables in main() will be modif=
ied because of reference.
I created Temporary class and I added it to Value class. The Temporary c=
lass treats like value type to preserve original reference before it can be=
modified inside operator= function.
Please check my code to see if I did anything correct. Please note that =
you may notice int Size in template. It is not used, but I will add it in =
some functions when I plan to implement my own.
Didn't you comment to say if my code is safe?
template< int Size >
class Value
{
private:
class Temporary
{
private:
int value;
public:
Temporary();
Temporary( Temporary const& right );
~Temporary();
typename Value< Size >::Temporary& operator=( typename Value< S=
ize >::Temporary const& right );
operator int () const;
typename Value< Size >::Temporary& operator=( int const& value =
);
int Get_Value() const;
void Set_Value( int value );
};
int &value; // reference to local variable in main()
public:
Value( int &value );
Value( Value< Size > const& right );
~Value();
Value< Size >& operator=( Value< Size > const& right );
Value< Size >& operator=( Temporary const& right );
operator int () const;
Value< Size >& operator=( int const& value );
int Get_Value() const;
void Set_Value( int value );
template< int Size >
friend typename Value< Size >::Temporary operator+( typename Value< S=
ize >::Temporary const left, Value< Size > const &right );
template< int Size >
friend typename Value< Size >::Temporary operator+( typename Value< S=
ize >::Temporary const left, int const& right );
template< int Size >
friend typename Value< Size >::Temporary operator+( int const& left, =
typename Value< Size >::Temporary const right );
template< int Size >
friend typename Value< Size >::Temporary operator+( Value< Size > con=
st& left, Value< Size > const& right );
template< int Size >
friend typename Value< Size >::Temporary operator+( Value< Size > con=
st& left, int const& right );
template< int Size >
friend typename Value< Size >::Temporary operator+( int const& left, =
Value< Size > const& right );
template< int Size >
friend Value< Size > &operator+=( Value< Size > &left, Value< Size =
const& right );
template< int Size >
friend Value< Size > &operator+=( Value< Size > &left, int const &r=
ight );
template< int Size >
friend Value< Size > &operator+=( Value< Size > &left, typename Val=
ue< Size >::Temporary const right );
};
template< int Size >
Value< Size >::Temporary::Temporary()
{
}
template< int Size >
Value< Size >::Temporary::Temporary( Temporary const& right ) : value( ri=
ght.value )
{
}
template< int Size >
Value< Size >::Temporary::~Temporary()
{
}
template< int Size >
typename Value< Size >::Temporary &Value< Size >::Temporary::operator=(=
typename Value< Size >::Temporary const &right )
{
value = right.value;
return *this;
}
template< int Size >
Value< Size >::Temporary::operator int () const
{
return value;
}
template< int Size >
typename Value< Size >::Temporary& Value< Size >::Temporary::operator=(=
int const& value )
{
this->value = value;
return *this;
}
template< int Size >
int Value< Size >::Temporary::Get_Value() const
{
return value;
}
template< int Size >
void Value< Size >::Temporary::Set_Value( int value )
{
this->value = value;
}
template< int Size >
typename Value< Size >::Temporary operator+( typename Value< Size >::Temp=
orary const left, Value< Size > const &right )
{
typename Value< Size >::Temporary temp;
temp.Set_Value( left.Get_Value() + right.Get_Value() );
return temp;
}
template< int Size >
typename Value< Size >::Temporary operator+( typename Value< Size >::Temp=
orary const left, int const& right )
{
Temporary temp;
temp.Set_Value( left.Get_Value() + right );
return temp;
}
template< int Size >
typename Value< Size >::Temporary operator+( int const& left, typename Va=
lue< Size >::Temporary const right )
{
Temporary temp;
temp.Set_Value( left + right.Get_Value() );
return temp;
}
template< int Size >
Value< Size >::Value( int &value ) : value( value )
{
}
template< int Size >
Value< Size >::Value( Value< Size > const& right ) : value( right.value )
{
}
template< int Size >
Value< Size >::~Value()
{
}
template< int Size >
Value< Size > &Value< Size >::operator=( Value< Size > const& right )
{
value = right.value;
return *this;
}
template< int Size >
Value< Size > &Value< Size >::operator=( typename Value< Size >::Tempor=
ary const& right )
{
value = right.Get_Value();
return *this;
}
template< int Size >
Value< Size >::operator int () const
{
return value;
}
template< int Size >
Value< Size >& Value< Size >::operator=( int const& value )
{
this->value = value;
return *this;
}
template< int Size >
int Value< Size >::Get_Value() const
{
return value;
}
template< int Size >
void Value< Size >::Set_Value( int value )
{
this->value = value;
}
template< int Size >
typename Value< Size >::Temporary operator+( Value< Size > const& left, V=
alue< Size > const& right )
{
typename Value< Size >::Temporary temp;
temp.Set_Value( left.Get_Value() + right.Get_Value() );
return temp;
}
template< int Size >
typename Value< Size >::Temporary operator+( Value< Size > const& left, i=
nt const& right )
{
typename Value< Size >::Temporary temp;
temp.Set_Value( left.Get_Value() + right );
return temp;
}
template< int Size >
typename Value< Size >::Temporary operator+( int const& left, Value< Size=
> const& right )
{
typename Value< Size >::Temporary temp;
temp.Set_Value(left + right.Get_Value() );
return temp;
}
template< int Size >
Value< Size > &operator+=( Value< Size > &left, Value< Size > const& ri=
ght )
{
left.Set_Value( left.Get_Value() + right.Get_Value() );
return left;
}
template< int Size >
Value< Size > &operator+=( Value< Size > &left, int const &right )
{
left.Set_Value( left.Get_Value() + right );
return left;
}
template< int Size >
Value< Size > &operator+=( Value< Size > &left, typename Value< Size >:=
:Temporary const right )
{
left.Set_Value( left.Get_Value() + right.Get_Value() );
return left;
}
int main()
{
int b1 = 1;
int b2 = 2;
int b3 = 4;
int b4 = 8;
Value< 1 > a1( b1 );
Value< 1 > a2( b2 );
Value< 1 > a3( b3 );
Value< 1 > a4( b4 );
a1 = a2 + a3 + a4; a1 = 1;
a1 = a2 + a3 + 8; a1 = 1;
a1 = a2 + 4 + a4; a1 = 1;
a1 = a2 + 4 + 8; a1 = 1;
a1 = 2 + a3 + a4; a1 = 1;
a1 = 2 + a3 + 8; a1 = 1;
a1 = 2 + 4 + a4; a1 = 1;
a1 = 2 + 4 + 8; a1 = 1;
a1 += a2 + a3 + a4; a1 = 1;
a1 += a2 + a3 + 8; a1 = 1;
a1 += a2 + 4 + a4; a1 = 1;
a1 += a2 + 4 + 8; a1 = 1;
a1 += 2 + a3 + a4; a1 = 1;
a1 += 2 + a3 + 8; a1 = 1;
a1 += 2 + 4 + a4; a1 = 1;
a1 += 2 + 4 + 8; a1 = 1;
a1 += a2; a1 = 1;
a1 += 1; a1 = 1;
return 0;
}