Operator Overloading and Interdependant Classes

S

SpoonfulofTactic

I am working on a new library for my own use that
would allow me to use SI Units and to convert
them back and forth with ease.

However, While I have been working on operator
overloading, I have come accross a problem:

If I want to be able to simplify units through
operators, I end up with inter-dependant classes.

1 class Area {
2 public:
3 Area operator+(const Area& addend);
4 Area& operator+=(const Area& addend);
5 Area operator*(const float scalar);
6 Area operator*(const int scalar);
7 Length operator/(const Length& addend);
8 };
9
10 class Length {
11 public:
12 Length operator+(const Length& addend);
13 Length& operator+=(const Length& addend);
14 Length operator*(const float scalar);
15 Length operator*(const int scalar);
16 Area operator*(const Length& multiplier);
17 };

This tells me that I cannot have no type on the "Length" token on line
7.

What am I to do?
 
K

Karl Heinz Buchegger

I am working on a new library for my own use that
would allow me to use SI Units and to convert
them back and forth with ease.

However, While I have been working on operator
overloading, I have come accross a problem:

If I want to be able to simplify units through
operators, I end up with inter-dependant classes.

1 class Area {
2 public:
3 Area operator+(const Area& addend);
4 Area& operator+=(const Area& addend);
5 Area operator*(const float scalar);
6 Area operator*(const int scalar);
7 Length operator/(const Length& addend);
8 };
9
10 class Length {
11 public:
12 Length operator+(const Length& addend);
13 Length& operator+=(const Length& addend);
14 Length operator*(const float scalar);
15 Length operator*(const int scalar);
16 Area operator*(const Length& multiplier);
17 };

This tells me that I cannot have no type on the "Length" token on line
7.

What am I to do?

use a forward declaration:

class Length; // forward declare that somewhere there is indeed a class
// called 'Length'

class Area {
public:
.....

Length operator/(const Length& addend);
// now that the compiler knows that 'Length' is not a typo, but
// indeed is the name of a class, this is valid
};

class Length {
public:
....
};
 
V

Victor Bazarov

I am working on a new library for my own use that
would allow me to use SI Units and to convert
them back and forth with ease.

However, While I have been working on operator
overloading, I have come accross a problem:

If I want to be able to simplify units through
operators, I end up with inter-dependant classes.

1 class Area {
2 public:
3 Area operator+(const Area& addend);
4 Area& operator+=(const Area& addend);
5 Area operator*(const float scalar);
6 Area operator*(const int scalar);
7 Length operator/(const Length& addend);
8 };
9
10 class Length {
11 public:
12 Length operator+(const Length& addend);
13 Length& operator+=(const Length& addend);
14 Length operator*(const float scalar);
15 Length operator*(const int scalar);
16 Area operator*(const Length& multiplier);
17 };

First of all, take a look at this:
---------------------------------------------
class Area {
public:
Area operator+(const Area& addend);
Area& operator+=(const Area& addend);
Area operator*(const float scalar);
Area operator*(const int scalar);
Length operator/(const Length& addend); //***** line 7
};

class Length {
public:
Length operator+(const Length& addend);
Length& operator+=(const Length& addend);
Length operator*(const float scalar);
Length operator*(const int scalar);
Area operator*(const Length& multiplier);
};
---------------------------------------------
Now, how much nicer is this text compared to yours? It looks and
reads as the C++ code it is.
This tells me that I cannot have no type on the "Length" token on line
7.

What am I to do?

Define both operator/ in Area and operator* in Length as non-members
_after_ both classes. They should be non-members, anyway, since they
don't change the members. They could be friends (although I am sure they
don't need to be).

V
 
O

Otac0n

First of All, Karl, thank you. Somehow I did not t=know that forward
declaration of classes was possible.

And secondly, Victor, I see absolutely no benefeit to your style.
 
V

Victor Bazarov

Otac0n said:
First of All, Karl, thank you. Somehow I did not t=know that forward
declaration of classes was possible.

And secondly, Victor, I see absolutely no benefeit to your style.

Too bad.
 
K

Kai-Uwe Bux

Otac0n said:
First of All, Karl, thank you. Somehow I did not t=know that forward
declaration of classes was possible.

And secondly, Victor, I see absolutely no benefeit to your style.

The benefit is that one can easily cut and past the code -- no need to get
rid of the line numbers before you try to compile.


Best

Kai-Uwe Bux
 
O

Otac0n

This was not executable code, and it needed a line number.

In this case, the style used is moot.
 
R

ravinderthakur

hi,

i think what victor was trying to say that the code should be in c++
because may of the people sititng out and helping ocassionaly paste the
code into their environment and
then check out the problem. so if the code is pure C++ it is better if
the code can be pasted and directly be runable.

thanks
rt
 
K

Karl Heinz Buchegger

Otac0n said:
This was not executable code, and it needed a line number.

In this case, the style used is moot.

For you: yes.
For us: no.

*We* are the ones who frequently cut&paste things *from* a
newsgroup posting into our editors to figure out what the
OP could have ment and what error messages he gets.

So making this job as easy as possible for us increases your
chances of getting an answer tremendously. In this case it
doesn't even mean more work for you: Just cut&paste the code
from your editor and mark the line in question. Is that asking
for to much?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top