Temporary Class through Reference

N

Nephi Immortal

I would like to demonstrate my code how Value class can use reference instead of value type. I may want to use helper class when two or more functions are connected together in one line such as F1().F2().F3()……

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 going to use this function, then some local variables in main() will be modified because of reference.

I created Temporary class and I added it to Value class. The Temporary class 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.

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< Size >::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< Size >::Temporary const left, Value< Size > const &right );

template< int Size >
friend typename Value< Size >::Temporary operator+( typename Value< Size >::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 > 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, 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 &right );

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( right.value )
{
}

template< int Size >
Value< Size >::Temporary::~Temporary()
{
}

template< int Size >
typename Value< Size >::Temporary &Value< Size >::Temporary::eek:perator=( typename Value< Size >::Temporary const &right )
{
value = right.value;
return *this;
}

template< int Size >
Value< Size >::Temporary::eek:perator int () const
{
return value;
}

template< int Size >
typename Value< Size >::Temporary& Value< Size >::Temporary::eek:perator=( 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 >::Temporary 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 >::Temporary 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 Value< 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 >::eek:perator=( Value< Size > const& right )
{
value = right.value;
return *this;
}

template< int Size >
Value< Size > &Value< Size >::eek:perator=( typename Value< Size >::Temporary const& right )
{
value = right.Get_Value();
return *this;
}

template< int Size >
Value< Size >::eek:perator int () const
{
return value;
}

template< int Size >
Value< Size >& Value< Size >::eek:perator=( 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, 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+( Value< Size > const& left, intconst& 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& right )
{
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;
}
 
N

Nephi Immortal

I would like to demonstrate my code how Value class can use reference instead of value type. I may want to use helper class when two or more functions are connected together in one line such as F1().F2().F3()……



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 going to use this function, then some local variables in main() will be modified because of reference.



I created Temporary class and I added it to Value class. The Temporary class treats like value type to preserve original reference before it can bemodified 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top