Operator Overloading and Interdependant Classes

Discussion in 'C++' started by SpoonfulofTactic@gmail.com, Aug 23, 2005.

  1. Guest

    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?
     
    , Aug 23, 2005
    #1
    1. Advertising

  2. 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+=(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:
    ....
    };


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 23, 2005
    #2
    1. Advertising

  3. 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+=(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
     
    Victor Bazarov, Aug 23, 2005
    #3
  4. Otac0n Guest

    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.
     
    Otac0n, Aug 23, 2005
    #4
  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.
    >


    Too bad.
     
    Victor Bazarov, Aug 23, 2005
    #5
  6. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Aug 23, 2005
    #6
  7. Otac0n Guest

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

    In this case, the style used is moot.
     
    Otac0n, Aug 23, 2005
    #7
  8. mlimber Guest

    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/tutorial/dimensional-analysis.html

    Cheers! --M
     
    mlimber, Aug 23, 2005
    #8
  9. Guest

    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, 2005
    #9
  10. 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
     
    Karl Heinz Buchegger, Aug 25, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Galina
    Replies:
    0
    Views:
    393
    Galina
    Dec 15, 2003
  2. Stimp
    Replies:
    1
    Views:
    292
    Stimp
    Dec 14, 2006
  3. Replies:
    11
    Views:
    562
  4. Replies:
    2
    Views:
    312
  5. Josh
    Replies:
    1
    Views:
    90
    Lasse Reichstein Nielsen
    Oct 23, 2003
Loading...

Share This Page