Re: Operatr+ Updated
On Feb 19, 4:42 pm, Ian Collins <ian-n...@hotmail.com> wrote:
On 02/20/11 09:04 AM, Nephi Immortal wrote:
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 const=
ructor
function. If I do, then copy constructor will have undefined
behavior. I should always place it in private.
Told by whom and in what context?
They said not to put const on any function s parameter if parame=
ter
is built-in type such as int.Please clarify why I should not use
> const?
That's because it doesn't have any meaning for a value parameter (the
same would apply for any type passed by value).
Const is necessary to prevent changing parameter and I always
create temporary local variable and copy const parameter to it.
That only applies if the parameter is a reference or a pointer. Both o=
f
these refer to objects in the scope of the caller, so declaring the
parameter const prevents the called function changing the original
value. Value parameters are copies of the original, Changing them has
no effect on the the original.
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 cla=
ss B is
inherited into _Low_Byte class and _High_Byte class. You need to
redefine operator=.
This is hard to parse.
I do not need to post two classes since you understood what my c=
ode
means.
I believe that class A is to be correct code design. What abo=
ut
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 );
These should be members, returning A&. += updates the current instan=
ce,
it doesn't create a new one.
private:
unsigned int m_data;
};
<snip>
class B {
public:
explicit B( unsigned int&data );
Why do you use an int& here?
I won't comment further, I'm not sure what B is doing.
I tried to explain above. Class B needs to use reference on
constructor and data member. I did not post two functions. It should
clarify clearly.
Class A {
....
B Low_Byte();
B High_Byte();
....
};
B Low_Byte() {
Return B( this->m_data );
}
B High_Byte() {
Return B( this->m_data );
}
Now, you see class B modifies class A's data member through reference
when Low_Byte() function is called. It looks like this.
A a( 2 ), b( 4 ), c( 0 );
c.Low_Byte() = a.Low_Byte() + b.Low_Byte();
You notice that class B's operator+ only returns unsigned int instead
of returning class B reference. Returning class B reference can cause
to modify class A's data member incorrectly through class B's copy
constructor since returning class B should be local and not
reference. Returning unsigned int is the answer and class B's
operator=( unsigned int ) is called before reference can modify class
A's data member correctly.
Am I saying clearly?
Please explain why you think operator+ should be member function. It
needs to be global function because it has three possible operator+.
Other folks explained this earlier.