473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator Overloading and Interdependant Classes

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+=(cons t 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+=(cons t 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?

Aug 23 '05 #1
9 2206
Sp************* *@gmail.com wrote:

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+=(cons t 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+=(cons t 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:
....
};
--
Karl Heinz Buchegger
kb******@gascad .at
Aug 23 '05 #2
Sp************* *@gmail.com wrote:
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+=(cons t 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+=(cons t 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+=(cons t 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+=(cons t 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
Aug 23 '05 #3
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.

Aug 23 '05 #4
Otac0n wrote:
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.
Aug 23 '05 #5
Otac0n wrote:
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
Aug 23 '05 #6
This was not executable code, and it needed a line number.

In this case, the style used is moot.

Aug 23 '05 #7
Sp************* *@gmail.com wrote:
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.

[snip]

You might be interested in the alternative approach to dimensional
analysis from the Boost MPL tutorial:

http://boost.org/libs/mpl/doc/tutori...-analysis.html

Cheers! --M

Aug 23 '05 #8
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

Aug 24 '05 #9
Otac0n wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 25 '05 #10

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

Similar topics

2
2065
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
3
3463
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based memory allocation? like complex<int> * c1,*c2,*c3; i still want to do something like c3 = c1+c2 ; rether than *c3 = *c1+*c2;
3
3278
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
14
2337
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a hash string class ) are derived. The two derived classes are template classes specifying the sizes. The base class is a non-template class so that it can be used generically in the interface classes. the design would look like
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.