Temporary Class through Reference

Discussion in 'C++' started by Nephi Immortal, Feb 5, 2013.

  1. 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;
    }
    Nephi Immortal, Feb 5, 2013
    #1
    1. Advertising

  2. 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 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?

    >
    >
    >
    > 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, 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& 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;
    >
    > }
    Nephi Immortal, Feb 7, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler
    Replies:
    2
    Views:
    613
    Alexander Stippler
    Jul 4, 2003
  2. poisondart
    Replies:
    6
    Views:
    417
    fred.dixon
    Apr 29, 2005
  3. siddhu
    Replies:
    12
    Views:
    621
    Gianni Mariani
    Jun 6, 2007
  4. Replies:
    7
    Views:
    3,180
    James Kanze
    Feb 12, 2008
  5. Nathan Craddock

    Web Reference class spawns temporary assembly when instantiated?

    Nathan Craddock, Oct 7, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    130
    Nathan C.
    Oct 13, 2005
Loading...

Share This Page