Temporary Class through Reference
I would like to demonstrate my code how Value class can use reference inste=
ad of value type. I may want to use helper class when two or more function=
s 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+= such =
as Value& operator+( Value const &left, Value const &right ). If I am goin=
g to use this function, then some local variables in main() will be modifie=
d because of reference.
I created Temporary class and I added it to Value class. The Temporary cla=
ss treats like value type to preserve original reference before it can be m=
odified inside operator= function.
Please check my code to see if I did anything correct. Please note that yo=
u may notice int Size in template. It is not used, but I will add it in so=
me functions when I plan to implement my own.
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< Siz=
e >::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< Siz=
e >::Temporary const left, Value< Size > const &right );
template< int Size >
friend typename Value< Size >::Temporary operator+( typename Value< Siz=
e >::Temporary const left, int const& right );
template< int Size >
friend typename Value< Size >::Temporary operator+( int const& left, ty=
pename Value< Size >::Temporary const right );
template< int Size >
friend typename Value< Size >::Temporary operator+( Value< Size > const=
& left, Value< Size > const& right );
template< int Size >
friend typename Value< Size >::Temporary operator+( Value< Size > const=
& left, int const& right );
template< int Size >
friend typename Value< Size >::Temporary operator+( int const& left, Va=
lue< 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 &rig=
ht );
template< int Size >
friend Value< Size > &operator+=( Value< Size > &left, typename Value=
< Size >::Temporary const right );
};
template< int Size >
Value< Size >::Temporary::Temporary()
{
}
template< int Size >
Value< Size >::Temporary::Temporary( Temporary const& right ) : value( righ=
t.value )
{
}
template< int Size >
Value< Size >::Temporary::~Temporary()
{
}
template< int Size >
typename Value< Size >::Temporary &Value< Size >::Temporary::operator=( t=
ypename 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=( i=
nt 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 >::Tempor=
ary 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 >::Tempor=
ary 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 Valu=
e< 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 >::Temporar=
y 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, Val=
ue< 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, int=
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& righ=
t )
{
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 >::T=
emporary 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;
}