user defined operator returns another type?

This is a discussion on user defined operator returns another type? within the C++ Programming forums, part of the General Programming Boards category; How can I define an operator for a class, which has a return value of another class? For example: class ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question user defined operator returns another type?

    How can I define an operator for a class, which has a return value of another class? For example:

    class 1: real
    class 2: imag
    addition between the two results in a third class: complex

    or maybe:
    class: integer
    division between two integers results in another class: fraction

    Or even better, if it could return a variable of type variant<integer, fraction>, if the division gets evenly out it should return an integer.

    Note: all classes in these examples are user defined.
    Come on, you can do it! b( ~_')

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    The same way you would overload it for a built in type or the class itself.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by prog-bman View Post
    The same way you would overload it for a built in type or the class itself.
    so

    Code:
    class real {
        double val;
    public:
        complex operator+(imag);
    };
    
    class imag {
        double val;
    };
    
    complex real::operator+(imag a) {
        return complex(val, a.val);
    }
    But this code wont work, I'll have to make val a public member of imag, right? But that feels a litte bit strange, it can be private in real, but it has to be public in imag... :-/
    Come on, you can do it! b( ~_')

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could make it a friend, but the better method is to provide a proper accessor. The imag class isn't very useful if there's no way to read or write the value, after all.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by CornedBee View Post
    You could make it a friend, but the better method is to provide a proper accessor. The imag class isn't very useful if there's no way to read or write the value, after all.
    There's really no reason for a "complex" class to have anything other than the real and imaginary parts in it. I'd just make them both public, then write the operators as standalone functions, no need for friend.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, it's not like there are any restrictions on the values, or any special storage methods.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    But what's the difference betweeen a class and a type if I make everyting public anway?
    Come on, you can do it! b( ~_')

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by TriKri View Post
    But what's the difference betweeen a class and a type if I make everyting public anway?
    C has structures which are always completely public. That hardly makes them useless. Data aggregation and encapsulation are two different concepts.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The choice of making the elements public or not in a class is something you have to seriously consider - the key question being:
    - Will the way that we store the internal representation of the data change.

    In the case of a complex class, the likelyhood is that you always will use double (or float) for the entire lifecycle of the application, because there really isn't any other good way to do that.

    The same applies if you are developing a game that uses a "Vector3" which has "float x, y, z" to describe the vector - you don't really need accessor functions for that.

    On the other hand, if you are to produce a string class, you wouldn't want to show the user how the string is stored internally - it is probably less useful in the first place, and you may want to store it differently in the future (e.g. if the string is often appended, you may have a linked list of string-segments, instead of re-allocating the string every time).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  4. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21