473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

difference in following code

For an exercise I had to write a class that would do math operations
on complex numbers. The main point of the exercise was overloading
various operators. One of them was the conjugate operator "~".

I overloaded it thus:

void Complecks::operator~(const Complecks& q)const{

im = - im;
}

I figured the invoking object would have it's "im" member negated,
which seemed to be the purpose. But that didn't work. Fortunately
for me, the author of this book gave the solution to this problem and
his method was:

Complecks Complecks::operator~(const Complecks& q)const{
Complecks t;
t.real = real - q.real;
t.im = im - q.im;
return t;
}

Okay. But the method is invoked with a call like " ~c " where c
is a complex number with a real and imaginary component, no
assignement operator is involved. So, doesn't the latter code just
return back to "c"? Why wouldn't the first code work the same?

Jun 20 '07 #1
9 1474
waltbrad wrote:
For an exercise I had to write a class that would do math operations
on complex numbers. The main point of the exercise was overloading
various operators. One of them was the conjugate operator "~".

I overloaded it thus:

void Complecks::operator~(const Complecks& q)const{

im = - im;
What's 'im'? Is that a member? Of what, 'q' or '*this'? If it's
(supposedly) the member of '*this', then you're trying to change the
'*this' object, right? And you just declared that object 'const' by
putting 'const' after the function declaration (before the curly
brace). So, even if the syntax were accepted (I don't think it
should be, since ~ is an unary operator, so it shouldn't have any
arguments), the compilation shouldn't let you change a const object.
}

I figured the invoking object would have it's "im" member negated,
which seemed to be the purpose. But that didn't work. Fortunately
for me, the author of this book
*What* book? I am asking so that we'd recommend people against it
since it doesn't seem to have correct C++ examples.
gave the solution to this problem and
his method was:

Complecks Complecks::operator~(const Complecks& q)const{
Complecks t;
t.real = real - q.real;
t.im = im - q.im;
return t;
}

Okay.
OKAY? Are you saying it will compile? How is 'Complecks' defined?
But the method is invoked with a call like " ~c " where c
is a complex number with a real and imaginary component, no
assignement operator is involved. So, doesn't the latter code just
return back to "c"?
No.
Why wouldn't the first code work the same?
It's up to you of course, but the operator~ (just like the operator!
or the operator-) is NOT supposed to change the object for which it
is called. It is supposed to return a new object:

Complecks Complecks::operator~() const {
return Complecks(real, -im); // there is a c-tor for that I hope
}

or

Complecks operator~(Complecks const& q) {
return Complecks(q.real(), -q.imaginary());
}

(supposing that you have defined 'Complecks' with needed constructor
and accessors).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 20 '07 #2

Victor Bazarov <v.********@comAcast.netwrote in message...

void Complecks::operator~(const Complecks& q)const{

im = - im;

What's 'im'? Is that a member? Of what, 'q' or '*this'? If it's
(supposedly) the member of '*this', then you're trying to change the
'*this' object, right? And you just declared that object 'const' by
putting 'const' after the function declaration (before the curly
brace). So, even if the syntax were accepted
** (I don't think it
should be, since ~ is an unary operator, so it shouldn't have any
arguments) ** ....
It should have one argument.

"TiCpp" v1 says:

" The bitwise not (~, also called the ones complement operator) is a unary
operator - it only takes ** one argument ** (all other bitwise operators are
binary operators). Bitwise not produces the opposite of the input bit - a
one if the input bit is zero, a zero if the input bit is one.
"
I think you meant that if a 'this' pointer is passed in, the '&q' arg makes
it two?
[ so make it 'static' member. <G]

--
Bob R
POVrookie
Jun 20 '07 #3
BobR wrote:
Victor Bazarov <v.********@comAcast.netwrote in message...
>>>
void Complecks::operator~(const Complecks& q)const{

im = - im;

What's 'im'? Is that a member? Of what, 'q' or '*this'? If it's
(supposedly) the member of '*this', then you're trying to change the
'*this' object, right? And you just declared that object 'const' by
putting 'const' after the function declaration (before the curly
brace). So, even if the syntax were accepted
>** (I don't think it
should be, since ~ is an unary operator, so it shouldn't have any
arguments) ** ....

It should have one argument.
A non-static member shouldn't have any _additional_ arguments. Some
(many) newbies do not realise that there is an implicit argument for
any non-static member function, so my note was for those.
"TiCpp" v1 says:

" The bitwise not (~, also called the ones complement operator) is a
unary operator - it only takes ** one argument ** (all other bitwise
operators are binary operators). Bitwise not produces the opposite of
the input bit - a one if the input bit is zero, a zero if the input
bit is one. "
I think you meant that if a 'this' pointer is passed in, the '&q' arg
makes it two?
[ so make it 'static' member. <G]
I think there may be a problem with that. Any overloaded operators
(except 'new', 'new[]', 'delete', and 'delete[]') should be either
non-static members or non-members.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '07 #4
On Jun 20, 4:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
waltbrad wrote:
For an exercise I had to write a class that would do math operations
on complex numbers. The main point of the exercise was overloading
various operators. One of them was the conjugate operator "~".
I overloaded it thus:
void Complecks::operator~(const Complecks& q)const{
im = - im;

What's 'im'? Is that a member? Of what, 'q' or '*this'? If it's
(supposedly) the member of '*this', then you're trying to change the
'*this' object, right? And you just declared that object 'const' by
putting 'const' after the function declaration (before the curly
brace). So, even if the syntax were accepted (I don't think it
should be, since ~ is an unary operator, so it shouldn't have any
arguments), the compilation shouldn't let you change a const object.

Hello. Sorry about the delayed response. I did follow up a yesterday
but for some reason my post never showed up.

Yes, 'im' is a member. Well, you have pretty much clarified things a
bit for me. Yes, I did think I was dealing with a 'this' pointer. And,
apparently there is one. But, I completely ignored that this was
const function.

I figured the invoking object would have it's "im" member negated,
which seemed to be the purpose. But that didn't work. Fortunately
for me, the author of this book

*What* book? I am asking so that we'd recommend people against it
since it doesn't seem to have correct C++ examples.
Stephen Prata's "C++ Primer Plus". It actually is not the book's
fault. The blame resides with my difficulty in grasping the material. C
++ is a very complicated language. I was trying to learn through
reading Deitel & Deitel's 4th edition, but six weeks and 600 pages
into that book I had to set it down. Then I turned to Prata and he is
at least giving me some handle on the language. But it still takes a
lot of work, a lot of time and a lot of practice. I also have tic++
on my harddrive and I will be reading that down the road. But over
half way through Prata. But, if you have a recommendation I'm open to
suggestions. "Accelerated C++" left me in the dust, though.

gave the solution to this problem and
his method was:
Complecks Complecks::operator~(const Complecks& q)const{
Complecks t;
t.real = real - q.real;
t.im = im - q.im;
return t;
}
Okay.

OKAY? Are you saying it will compile? How is 'Complecks' defined?
Yes, this code compiles and runs fine. Why not? It does create a new
object. Unless you mean that the expressions imply that there is
indeed a "this" pointer. I guess there does have to be a "this"
pointer.

================
class Complecks{
private:
double real;
double im;

public:
Complecks();
Complecks(double p, double q);
Complecks operator+(const Complecks& q)const;
Complecks operator-(const Complecks& q)const;
Complecks operator*(const Complecks& q)const;
Complecks operator~();
friend Complecks operator*(double x, const Complecks& q);
friend std::ostream& operator<<(std::ostream& os, const Complecks&
q);
friend std::istream& operator>>(std::istream& is, Complecks& q);
};
===========================
and then...

===========
//one of two constructors:

Complecks::Complecks(double p, double q){
real = p;
im = q;
}

//and the final way I implemented the "~" operator:

Complecks Complecks::operator~(){
Complecks obj;
obj.real = real;
obj.im = -im;
return obj;
}
================

I don't know if this is all you want to see or not. But apparently
there is a "this" operator.
But the method is invoked with a call like " ~c " where c
is a complex number with a real and imaginary component, no
assignement operator is involved. So, doesn't the latter code just
return back to "c"?

No.
Right. Here I was wrong. Although the example program didn't use "~c"
with an assignment operator, it could have done that. Instead the "~c"
was used in an ostream, and it returned the value to the ostream.
But, I guess the only reason the overloaded operator wouldn't return a
value with the first code was that it was declared void and constant.

Why wouldn't the first code work the same?

It's up to you of course, but the operator~ (just like the operator!
or the operator-) is NOT supposed to change the object for which it
is called. It is supposed to return a new object:

Complecks Complecks::operator~() const {
return Complecks(real, -im); // there is a c-tor for that I hope
}
Yes, your code would also work.
or

Complecks operator~(Complecks const& q) {
return Complecks(q.real(), -q.imaginary());
}

(supposing that you have defined 'Complecks' with needed constructor
and accessors).
This wouldn't work with the code I have, but yes, I can see how that
would work with the right setup.

Thanks for your input, I think this has cleared up some of my
understanding. But, do you agree that the overloaded ~ operator
function does have a "this" pointer?

Jun 22 '07 #5
waltbrad wrote:
On Jun 20, 4:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>waltbrad wrote:
>>For an exercise I had to write a class that would do math operations
on complex numbers. The main point of the exercise was overloading
various operators. One of them was the conjugate operator "~".
>>I overloaded it thus:
>>void Complecks::operator~(const Complecks& q)const{
^^^^^^^^^^^^^^^^^^
You seem to have completely ignored my comment about the argument here.
No matter.
>>
>>im = - im;

What's 'im'? Is that a member? Of what, 'q' or '*this'? If it's
(supposedly) the member of '*this', then you're trying to change the
'*this' object, right? And you just declared that object 'const' by
putting 'const' after the function declaration (before the curly
brace). So, even if the syntax were accepted (I don't think it
should be, since ~ is an unary operator, so it shouldn't have any
arguments), the compilation shouldn't let you change a const object.


Hello. Sorry about the delayed response. I did follow up a yesterday
but for some reason my post never showed up.

Yes, 'im' is a member. Well, you have pretty much clarified things a
bit for me. Yes, I did think I was dealing with a 'this' pointer. And,
apparently there is one. But, I completely ignored that this was
const function.

>>I figured the invoking object would have it's "im" member negated,
which seemed to be the purpose. But that didn't work. Fortunately
for me, the author of this book

*What* book? I am asking so that we'd recommend people against it
since it doesn't seem to have correct C++ examples.

Stephen Prata's "C++ Primer Plus". It actually is not the book's
fault. The blame resides with my difficulty in grasping the material.
C ++ is a very complicated language. I was trying to learn through
reading Deitel & Deitel's 4th edition, but six weeks and 600 pages
into that book I had to set it down. Then I turned to Prata and he is
at least giving me some handle on the language. But it still takes a
lot of work, a lot of time and a lot of practice. I also have tic++
on my harddrive and I will be reading that down the road. But over
half way through Prata. But, if you have a recommendation I'm open to
suggestions. "Accelerated C++" left me in the dust, though.

>>gave the solution to this problem and
his method was:
>>Complecks Complecks::operator~(const Complecks& q)const{
Complecks t;
t.real = real - q.real;
t.im = im - q.im;
return t;
}
>>Okay.

OKAY? Are you saying it will compile? How is 'Complecks' defined?
Yes, this code compiles and runs fine. Why not?
<shrug The code you posted earlier and the one you posted later
are _different_. Do you really want to know why the code you posted
originally shouldn't compile? Well, for one, operator ~ is unary,
if it's defined as a member, like you showed originally, it should
NOT have any explicit arguments (like in the code you posted later).
And I already told you that...
It does create a new
object. Unless you mean that the expressions imply that there is
indeed a "this" pointer. I guess there does have to be a "this"
pointer.

================
class Complecks{
private:
double real;
double im;

public:
Complecks();
Complecks(double p, double q);
Complecks operator+(const Complecks& q)const;
Complecks operator-(const Complecks& q)const;
Complecks operator*(const Complecks& q)const;
Complecks operator~();
^^^
Can you see that there is no argument here? Now look at your
original post, please.
friend Complecks operator*(double x, const Complecks& q);
friend std::ostream& operator<<(std::ostream& os, const Complecks&
q);
friend std::istream& operator>>(std::istream& is, Complecks& q);
};
===========================
and then...

===========
//one of two constructors:

Complecks::Complecks(double p, double q){
real = p;
im = q;
}

//and the final way I implemented the "~" operator:

Complecks Complecks::operator~(){
^^^^^^^^^
And no argument here either?
Complecks obj;
obj.real = real;
obj.im = -im;
return obj;
}
================

I don't know if this is all you want to see or not.
I want _you_ to see it.
But apparently
there is a "this" operator.

[..], do you agree that the overloaded ~ operator
function does have a "this" pointer?
Since the operator is defines as a non-static member function,
there can be no doubt about it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #6
On Jun 22, 4:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
You seem to have completely ignored my comment about the argument here.
No matter.


And no argument here either?
Complecks obj;
obj.real = real;
obj.im = -im;
return obj;
}
================
I don't know if this is all you want to see or not.

I want _you_ to see it.
But apparently
there is a "this" operator.
[..], do you agree that the overloaded ~ operator
function does have a "this" pointer?

Since the operator is defines as a non-static member function,
there can be no doubt about it.
Hmmmm... Sorry, Victor. I guess I have a little too much code laying
around. I'll have to look at this again a little bit later. Thanks.

Jun 22 '07 #7

Victor Bazarov wrote in message...
BobR wrote:
[ so make it 'static' member. <G]

I think there may be a problem with that. Any overloaded operators
(except 'new', 'new[]', 'delete', and 'delete[]') should be either
non-static members or non-members.
Yeah, Mr. Eckel demonstrates the overloads using 'friend', but lately, that
style seems to be getting the dumpster. [1]

You did notice the grin (<G>) in there, didn't you? ;-}

[1] reference: TiCpp v1

file://: C12:OverloadingUnaryOperators.cpp
// Non-member functions:
class Integer {
long i;
Integer* This() { return this; }
public:
Integer(long ll = 0) : i(ll) {}
// No side effects takes const& argument:
friend const Integer& operator+(const Integer& a);
friend const Integer operator-(const Integer& a);
friend const Integer operator~(const Integer& a);
// snip rest
};

// snip rest
const Integer operator~(const Integer& a) {
cout << "~Integer\n";
return Integer(~a.i);
}
// snip rest
--
Bob R
POVrookie
Jun 23 '07 #8
Yes, I definitely botched that up. That was not the author's solution
that I printed in the first post. That was my misreading of his
solution. I guess I was relying too much on a rote of understanding of
how to overload these operators. It worked for the binary operators,
but the holes in my understanding really showed through with the
unary.

So, if you'll bear with me here I just want to recap my "current"
understanding and see if you'll verify that I have this right.

Given that A, B and C are fellow class objects the statement:

C = A + B;

could be written as the following function call, (given that the
operator was overloaded correctly):

C = A.operator+(B);

But with a unary operator, the statement:

~c

would have a function call like:

c.operator~();

So, c is the invoking object and the "this" operator contains the
address of c.

Further, the function definition for the overloaded unary operator:

* creates a new object that it returns; so the return type could not
be "void".

* the purpose of any of the overloaded operators is to return a new
object; so the function does NOT alter the invoking object.

* the overloaded unary operator will only have ONE argument if it is
declared as a friend funtion. But as a member function it has no
argument.

Sorry for being such a pain. I do believe I carry a greater
understanding of these operators in general now though.

Thanks again.


Jun 23 '07 #9
waltbrad wrote:
Yes, I definitely botched that up. That was not the author's solution
that I printed in the first post. That was my misreading of his
solution. I guess I was relying too much on a rote of understanding of
how to overload these operators. It worked for the binary operators,
but the holes in my understanding really showed through with the
unary.

So, if you'll bear with me here I just want to recap my "current"
understanding and see if you'll verify that I have this right.

Given that A, B and C are fellow class objects the statement:

C = A + B;

could be written as the following function call, (given that the
operator was overloaded correctly):

C = A.operator+(B);
That presumes the operator was overloaded as a member. It does not
have to have been. It could be overloaded as a non-member, then the
actual call is

C = operator+(A, B);
>
But with a unary operator, the statement:

~c

would have a function call like:

c.operator~();
Again, that's if it's a member.
So, c is the invoking object and the "this" operator contains the
address of c.
Right.
Further, the function definition for the overloaded unary operator:

* creates a new object that it returns; so the return type could not
be "void".
It's up to the designer. If the return value is supposed to designate
the result, it should return it.
* the purpose of any of the overloaded operators is to return a new
object; so the function does NOT alter the invoking object.
That's not necessarily true. Pre-increment operator, for example,
does change the value of the object for which it's invoked.
* the overloaded unary operator will only have ONE argument if it is
declared as a friend funtion. But as a member function it has no
argument.
...has no *explicit* argument.
Sorry for being such a pain. I do believe I carry a greater
understanding of these operators in general now though.
Good. Glad to be of help.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neil Rutherford | last post by:
During testing of an application, i noticed a difference between SQL 2000 and SQL 7, both with identical config. In a nutshell: A table has a trigger for UPDATE and DELETE. When a column in the...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
3
by: Anoj Kumar | last post by:
Hi All , can anyone tell me what is the difference between the following declaration and how it affects application performance. 1. Dim cn As ADODB.Connection Set cn = New ADODB.Connection
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.