N
Nephi Immortal
I read the topic – "Error operator+ function” in the previous
thread. Other folks explained how to put three possible operator+
functions.
I wrote two versions of class A and class B. Both classes are
similar, but one data member of class B uses reference instead of
value.
I cannot use class A version. I am told not to use copy constructor
function. If I do, then copy constructor will have undefined
behavior. I should always place it in private.
They said not to put const on any function’s parameter if parameter
is built-in type such as int. Please clarify why I should not use
const? Const is necessary to prevent changing parameter and I always
create temporary local variable and copy const parameter to it.
Take a look at class A and class B. Compare both of them. Class B
is ideal to be helper class or proxy class.
You can write like
_Low_Byte Low_Byte()
and
_High_Byte High_Byte()
Both functions are in class A.
You can create two classes: _Low_Byte and _High_Byte. The class B is
inherited into _Low_Byte class and _High_Byte class. You need to
redefine operator=.
I do not need to post two classes since you understood what my code
means.
I believe that class A is to be correct code design. What about
class B?
class A {
public:
explicit A( unsigned int data );
~A();
A( const A &right );
A &operator=( const A &right );
A &operator=( unsigned int data );
operator unsigned int();
friend A operator+( const A &left, const A &right );
friend A operator+( const A &left, unsigned int right );
friend A operator+( unsigned int left, const A &right );
friend A operator+=( const A &left, const A &right );
friend A operator+=( const A &left, unsigned int right );
friend A operator+=( unsigned int left, const A &right );
private:
unsigned int m_data;
};
A::A( unsigned int data ) : m_data( data ) {
}
A::~A() {
}
A::A( const A &right ) : m_data( right.m_data ) {
}
A &A:perator=( const A &right ) {
m_data = right.m_data;
return *this;
}
A &A:perator=( unsigned int data ) {
m_data = data;
return *this;
}
A:perator unsigned int() {
return m_data;
}
A operator+( const A &left, const A &right ) {
return A( left ) += right;
}
A operator+( const A &left, unsigned int right ) {
return A( left ) += right;
}
A operator+( unsigned int left, const A &right ) {
return left += A( right );
}
A operator+=( const A &left, const A &right ) {
A t( left );
t.m_data += right.m_data;
return t;
}
A operator+=( const A &left, unsigned int right ) {
A t( left );
t.m_data += right;
return t;
}
A operator+=( unsigned int left, const A &right ) {
A t( right );
t.m_data += left;
return t;
}
class B {
public:
explicit B( unsigned int &data );
~B();
B( const B &right );
B &operator=( const B &right );
B &operator=( unsigned int data );
operator unsigned int();
friend unsigned int operator+( const B &left, const B &right );
friend unsigned int operator+( const B &left, unsigned int right );
friend unsigned int operator+( unsigned int left, const B &right );
friend unsigned int operator+=( const B &left, const B &right );
friend unsigned int operator+=( const B &left, unsigned int right );
friend unsigned int operator+=( unsigned int left, const B &right );
private:
unsigned int &m_data;
};
B::B( unsigned int &data ) : m_data( data ) {
}
B::~B() {
}
B::B( const B &right ) : m_data( right.m_data ) {
}
B &B:perator=( const B &right ) {
m_data &= 0xFF00u;
m_data |= ( right.m_data & 0xFFu );
return *this;
}
B &B:perator=( unsigned int data ) {
m_data &= 0xFF00u;
m_data |= ( data & 0xFFu );
return *this;
}
B:perator unsigned int() {
return m_data & 0xFFu;
}
unsigned int operator+( const B &left, const B &right ) {
unsigned int t = left.m_data;
t += right.m_data;
return t;
}
unsigned int operator+( const B &left, unsigned int right ) {
unsigned int t = left.m_data;
t += right;
return t;
}
unsigned int operator+( unsigned int left, const B &right ) {
unsigned int t = right.m_data;
t += left;
return t;
}
unsigned int operator+=( const B &left, const B &right ) {
unsigned int t = left.m_data;
t += right.m_data;
return t;
}
unsigned int operator+=( const B &left, unsigned int right ) {
unsigned int t = left.m_data;
t += right;
return t;
}
unsigned int operator+=( unsigned int left, const B &right ) {
unsigned int t = right.m_data;
t += left;
return t;
}
int main () {
A a( 2 ), b( 4 ), c( 0 );
c = a + b;
c = a + 5u;
c = 7u + b;
unsigned int data = 0x1234u, data2 = 0x5678u, data3 = 0xCCCCu;
B d( data ), e( data2 ), f( data3 );
f = d + e;
f = d + 0x10u;
f = 0x30u + e;
return 0;
}
thread. Other folks explained how to put three possible operator+
functions.
I wrote two versions of class A and class B. Both classes are
similar, but one data member of class B uses reference instead of
value.
I cannot use class A version. I am told not to use copy constructor
function. If I do, then copy constructor will have undefined
behavior. I should always place it in private.
They said not to put const on any function’s parameter if parameter
is built-in type such as int. Please clarify why I should not use
const? Const is necessary to prevent changing parameter and I always
create temporary local variable and copy const parameter to it.
Take a look at class A and class B. Compare both of them. Class B
is ideal to be helper class or proxy class.
You can write like
_Low_Byte Low_Byte()
and
_High_Byte High_Byte()
Both functions are in class A.
You can create two classes: _Low_Byte and _High_Byte. The class B is
inherited into _Low_Byte class and _High_Byte class. You need to
redefine operator=.
I do not need to post two classes since you understood what my code
means.
I believe that class A is to be correct code design. What about
class B?
class A {
public:
explicit A( unsigned int data );
~A();
A( const A &right );
A &operator=( const A &right );
A &operator=( unsigned int data );
operator unsigned int();
friend A operator+( const A &left, const A &right );
friend A operator+( const A &left, unsigned int right );
friend A operator+( unsigned int left, const A &right );
friend A operator+=( const A &left, const A &right );
friend A operator+=( const A &left, unsigned int right );
friend A operator+=( unsigned int left, const A &right );
private:
unsigned int m_data;
};
A::A( unsigned int data ) : m_data( data ) {
}
A::~A() {
}
A::A( const A &right ) : m_data( right.m_data ) {
}
A &A:perator=( const A &right ) {
m_data = right.m_data;
return *this;
}
A &A:perator=( unsigned int data ) {
m_data = data;
return *this;
}
A:perator unsigned int() {
return m_data;
}
A operator+( const A &left, const A &right ) {
return A( left ) += right;
}
A operator+( const A &left, unsigned int right ) {
return A( left ) += right;
}
A operator+( unsigned int left, const A &right ) {
return left += A( right );
}
A operator+=( const A &left, const A &right ) {
A t( left );
t.m_data += right.m_data;
return t;
}
A operator+=( const A &left, unsigned int right ) {
A t( left );
t.m_data += right;
return t;
}
A operator+=( unsigned int left, const A &right ) {
A t( right );
t.m_data += left;
return t;
}
class B {
public:
explicit B( unsigned int &data );
~B();
B( const B &right );
B &operator=( const B &right );
B &operator=( unsigned int data );
operator unsigned int();
friend unsigned int operator+( const B &left, const B &right );
friend unsigned int operator+( const B &left, unsigned int right );
friend unsigned int operator+( unsigned int left, const B &right );
friend unsigned int operator+=( const B &left, const B &right );
friend unsigned int operator+=( const B &left, unsigned int right );
friend unsigned int operator+=( unsigned int left, const B &right );
private:
unsigned int &m_data;
};
B::B( unsigned int &data ) : m_data( data ) {
}
B::~B() {
}
B::B( const B &right ) : m_data( right.m_data ) {
}
B &B:perator=( const B &right ) {
m_data &= 0xFF00u;
m_data |= ( right.m_data & 0xFFu );
return *this;
}
B &B:perator=( unsigned int data ) {
m_data &= 0xFF00u;
m_data |= ( data & 0xFFu );
return *this;
}
B:perator unsigned int() {
return m_data & 0xFFu;
}
unsigned int operator+( const B &left, const B &right ) {
unsigned int t = left.m_data;
t += right.m_data;
return t;
}
unsigned int operator+( const B &left, unsigned int right ) {
unsigned int t = left.m_data;
t += right;
return t;
}
unsigned int operator+( unsigned int left, const B &right ) {
unsigned int t = right.m_data;
t += left;
return t;
}
unsigned int operator+=( const B &left, const B &right ) {
unsigned int t = left.m_data;
t += right.m_data;
return t;
}
unsigned int operator+=( const B &left, unsigned int right ) {
unsigned int t = left.m_data;
t += right;
return t;
}
unsigned int operator+=( unsigned int left, const B &right ) {
unsigned int t = right.m_data;
t += left;
return t;
}
int main () {
A a( 2 ), b( 4 ), c( 0 );
c = a + b;
c = a + 5u;
c = 7u + b;
unsigned int data = 0x1234u, data2 = 0x5678u, data3 = 0xCCCCu;
B d( data ), e( data2 ), f( data3 );
f = d + e;
f = d + 0x10u;
f = 0x30u + e;
return 0;
}