N
Nephi Immortal
I create one main class. I use three data members to do addition
between left value and right value through reference. It is the
standard C++ code as common practice.
Look at the code below and see my comments.
class Data
{
public:
Data() : x( 0 ), y( 0 ), z( 0 )
{
}
Data( int _x, int _y, int _z ) : x( _x ), y( _y ), z( _z )
{
}
~Data()
{
}
Data( const Data &rData ) : x( rData.x ), y( rData.y ), z( rData.z )
{
}
Data &operator=( const Data &rData )
{
if( this == &rData )
return *this;
x = rData.x;
y = rData.y;
z = rData.z;
return *this;
}
Data &operator+=( const Data &rData )
{
x += rData.x;
y += rData.y;
z += rData.z;
return *this;
}
Data operator+( const Data &rData )
{
Data temp( *this );
return ( temp += rData );
}
private:
int x;
int y;
int z;
};
int main()
{
Data d1( 1, 2, 3 ), d2( 4, 5, 6 ), d3( 7, 8, 9 );
d1 = d2 + d3;
return 0;
}
My code looks fine. I decide to design nested classes inside main
class. They are inner classes and are hidden from the client. The
client uses Proxy() function to access nested class’ functions.
I create two helper classes. Both of them are tied or related
together. It is necessary because I put reference on data member:
rData. The Data class remains unmodified through Proxy class’
reference unless you never place any Object class’ data members inside
Proxy class or they will be modified.
I choose reference instead of copy of value because if I add more
data members in Data class, constructor function will copy all data
members every time it is called and destroyed. It would be overhead
on CPU.
I traced my code through debugger and tested all data members being
pushed into and popped out of stack in the correct order. Everything
is working properly.
How can you assure that my code is NOT undefined behavior?
class Object
{
private:
class _Proxy;
class Data
{
public:
Data() : x( 0 ), y( 0 ), z( 0 )
{
}
Data( int _x, int _y, int _z ) : x( _x ), y( _y ), z( _z )
{
}
~Data()
{
}
Data( const Data &_rData ) : x( _rData.x ), y( _rData.y ),
z( _rData.z )
{
}
Data &operator=( const Data &_rData )
{
if( this == &_rData )
return *this;
x = _rData.x;
y = _rData.y;
z = _rData.z;
return *this;
}
Data &operator+=( const Data &_rData )
{
x += _rData.x;
y += _rData.y;
z += _rData.z;
return *this;
}
Data operator+( const _Proxy &_rProxy )
{
Data temp( *this );
return ( temp += _rProxy.rData );
}
//private: /* Put Data class inside outer class. */
int x;
int y;
int z;
};
Data _data;
class _Proxy
{
public:
_Proxy( Data &_data ) : rData( _data )
{
}
~_Proxy()
{
}
_Proxy &operator=( const _Proxy &_rProxy )
{
if( this == &_rProxy )
return *this;
rData = _rProxy.rData;
return *this;
}
_Proxy &operator=( const Data &_rData )
{
rData = _rData;
return *this;
}
_Proxy &operator+=( const _Proxy &_rProxy )
{
rData += _rProxy.rData;
return *this;
}
Data operator+( const _Proxy &_rProxy )
{
Data temp( rData );
temp += _rProxy.rData;
return temp;
}
//private: /* Put _Proxy class inside outer class. */
Data &rData; // Reference
};
public:
Object() : _data( 0, 0, 0 )
{
}
Object( int _x, int _y, int _z ) : _data( _x, _y, _z )
{
}
~Object()
{
}
Object( const Object &rObject ) : _data( rObject._data )
{
}
Object &operator=( const Object &rObject )
{
if( this == &rObject )
return *this;
_data = rObject._data;
return *this;
}
_Proxy Proxy() // _Proxy is copy of value
{
return _Proxy( _data ); // _data is reference
}
};
int main()
{
Object object1, object2( 1, 2, 3 ), object3( 4, 5, 6 ), object4( 7,
8, 9 ), object5( 10, 11, 12 );
object1.Proxy() = object2.Proxy() + object3.Proxy() + object4.Proxy()
+ object5.Proxy();
object2.Proxy() = object3.Proxy();
object2.Proxy() += object4.Proxy();
object4 = object1;
return 0;
}
between left value and right value through reference. It is the
standard C++ code as common practice.
Look at the code below and see my comments.
class Data
{
public:
Data() : x( 0 ), y( 0 ), z( 0 )
{
}
Data( int _x, int _y, int _z ) : x( _x ), y( _y ), z( _z )
{
}
~Data()
{
}
Data( const Data &rData ) : x( rData.x ), y( rData.y ), z( rData.z )
{
}
Data &operator=( const Data &rData )
{
if( this == &rData )
return *this;
x = rData.x;
y = rData.y;
z = rData.z;
return *this;
}
Data &operator+=( const Data &rData )
{
x += rData.x;
y += rData.y;
z += rData.z;
return *this;
}
Data operator+( const Data &rData )
{
Data temp( *this );
return ( temp += rData );
}
private:
int x;
int y;
int z;
};
int main()
{
Data d1( 1, 2, 3 ), d2( 4, 5, 6 ), d3( 7, 8, 9 );
d1 = d2 + d3;
return 0;
}
My code looks fine. I decide to design nested classes inside main
class. They are inner classes and are hidden from the client. The
client uses Proxy() function to access nested class’ functions.
I create two helper classes. Both of them are tied or related
together. It is necessary because I put reference on data member:
rData. The Data class remains unmodified through Proxy class’
reference unless you never place any Object class’ data members inside
Proxy class or they will be modified.
I choose reference instead of copy of value because if I add more
data members in Data class, constructor function will copy all data
members every time it is called and destroyed. It would be overhead
on CPU.
I traced my code through debugger and tested all data members being
pushed into and popped out of stack in the correct order. Everything
is working properly.
How can you assure that my code is NOT undefined behavior?
class Object
{
private:
class _Proxy;
class Data
{
public:
Data() : x( 0 ), y( 0 ), z( 0 )
{
}
Data( int _x, int _y, int _z ) : x( _x ), y( _y ), z( _z )
{
}
~Data()
{
}
Data( const Data &_rData ) : x( _rData.x ), y( _rData.y ),
z( _rData.z )
{
}
Data &operator=( const Data &_rData )
{
if( this == &_rData )
return *this;
x = _rData.x;
y = _rData.y;
z = _rData.z;
return *this;
}
Data &operator+=( const Data &_rData )
{
x += _rData.x;
y += _rData.y;
z += _rData.z;
return *this;
}
Data operator+( const _Proxy &_rProxy )
{
Data temp( *this );
return ( temp += _rProxy.rData );
}
//private: /* Put Data class inside outer class. */
int x;
int y;
int z;
};
Data _data;
class _Proxy
{
public:
_Proxy( Data &_data ) : rData( _data )
{
}
~_Proxy()
{
}
_Proxy &operator=( const _Proxy &_rProxy )
{
if( this == &_rProxy )
return *this;
rData = _rProxy.rData;
return *this;
}
_Proxy &operator=( const Data &_rData )
{
rData = _rData;
return *this;
}
_Proxy &operator+=( const _Proxy &_rProxy )
{
rData += _rProxy.rData;
return *this;
}
Data operator+( const _Proxy &_rProxy )
{
Data temp( rData );
temp += _rProxy.rData;
return temp;
}
//private: /* Put _Proxy class inside outer class. */
Data &rData; // Reference
};
public:
Object() : _data( 0, 0, 0 )
{
}
Object( int _x, int _y, int _z ) : _data( _x, _y, _z )
{
}
~Object()
{
}
Object( const Object &rObject ) : _data( rObject._data )
{
}
Object &operator=( const Object &rObject )
{
if( this == &rObject )
return *this;
_data = rObject._data;
return *this;
}
_Proxy Proxy() // _Proxy is copy of value
{
return _Proxy( _data ); // _data is reference
}
};
int main()
{
Object object1, object2( 1, 2, 3 ), object3( 4, 5, 6 ), object4( 7,
8, 9 ), object5( 10, 11, 12 );
object1.Proxy() = object2.Proxy() + object3.Proxy() + object4.Proxy()
+ object5.Proxy();
object2.Proxy() = object3.Proxy();
object2.Proxy() += object4.Proxy();
object4 = object1;
return 0;
}