Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Temporary Class through Reference

Reply
Thread Tools

Temporary Class through Reference

 
 
Nephi Immortal
Guest
Posts: n/a
 
      02-05-2013
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:perator=( typename Value< Size >::Temporary const &right )
{
value = right.value;
return *this;
}

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

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

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

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

template< int Size >
Value< Size >& Value< Size >: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;
}
 
Reply With Quote
 
 
 
 
Nephi Immortal
Guest
Posts: n/a
 
      02-07-2013
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:perator=(typename Value< Size >::Temporary const &right )
>
> {
>
> value = right.value;
>
> return *this;
>
> }
>
>
>
> template< int Size >
>
> Value< Size >::Temporary:perator int () const
>
> {
>
> return value;
>
> }
>
>
>
> template< int Size >
>
> typename Value< Size >::Temporary& Value< Size >::Temporary: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 >:perator=( Value< Size > const& right )
>
> {
>
> value = right.value;
>
> return *this;
>
> }
>
>
>
> template< int Size >
>
> Value< Size > &Value< Size >:perator=( typename Value< Size >::Temporary const& right )
>
> {
>
> value = right.Get_Value();
>
> return *this;
>
> }
>
>
>
> template< int Size >
>
> Value< Size >:perator int () const
>
> {
>
> return value;
>
> }
>
>
>
> template< int Size >
>
> Value< Size >& Value< Size >: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;
>
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Reference assignment through base class reference siddhu C++ 12 06-06-2007 03:32 PM
Web Reference class spawns temporary assembly when instantiated? Nathan Craddock ASP .Net Web Services 1 10-13-2005 03:10 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
dynamically generating temporary files through python/cgi poisondart Python 6 04-28-2005 11:52 PM



Advertisments