Returning multiple types w/o a struct

This is a discussion on Returning multiple types w/o a struct within the C++ Programming forums, part of the General Programming Boards category; All right, this isn't really for me, but for a friend in my AP Computer science class... we're sort of ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Returning multiple types w/o a struct

    All right, this isn't really for me, but for a friend in my AP Computer science class... we're sort of off topic right now in the class since we get to do what we want as long as we keep up.

    He's working on a class, and he overloaded the [] operator. He wanted it so that if you had

    object[1] that it would return an apstring object, and any other number would return a float.

    His reason for doing this is that he is making his class like a struct and wants to use a for loop to get all the data.

    He found varients, but those are MFC and we're still using console apps.

    I suggested he had a different overload: [] (float) so that 1.0 would work, but he said he wanted the for loop, and if you did that then you couldn't send it as a float.

    Is there any way to do this, or if necessary, to make it so that it returns an apstring (converts the floats to apstring, we know how to do that) and then the overload (or another function) returns the data?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I'm not sure what you mean. Use a union I guess:

    Code:
    union ReturnType
    {
        apstring *strValue;
        int intValue;
        bool boolValue;
        float floatValue;
    };
    That whole chuck takes up the size of the biggest variable in there, which they're both the same on a IA-32 system (32 bits). So it'd take 4 bytes, and acts just like a struct.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I don't know what a union is.

    Basically, if this was used:

    cout << object[1] << endl;

    it needs to have the overload return an apstring.

    However, if any other number was in between the [ and ], it needs to return a float.

    The problem is this:

    If you specify a data type for a function, you have to use it. So either the overload can return an apstring or a float, but the only way I can see to do this is to have two functions.

    But if you have to use a float in the []s like this: [1.0], it defeats the whole purpose.

    The whole idea was to only return one data type, but it depends on the number passed to the overload

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Return a structure of this form. (You might want to consider redesigning your program because this is kinda sketchy. Unless of course, your teacher assigned it this way.)

    Code:
    struct foo
    {     apstring myString;//would hold the apstring
          float myNumber;//would hold the float
          bool datatype;//false could mean use the string, true could mean use the float
    }
    Then, when in the overloaded operator function, set datatype to the appropriate value

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    once again, he only wants to return ONE data type, not 3. Its just that the data type that needs to be returned is different based on the number inside the [ ]s. If it is a 1, then it returns A apstring, otherwise it returns A float.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You can't return multiple data types like that. In a real OOP language, like Java, you could return the an Object instance which stands for everything. But in C++, since you can't subclass primitive values and probably not allowed to modify apstring, you must use a struct/union/class. I suggest you use a union. It acts just like a struct (in coding principles -- no functions though) except it takes much less space in memory. A union is a structure where you can put many variables in but they all have the SAME memory address. So the size of a union is the size of the biggest element in the union. So if you want to return an apstring or a float, put them in a union like I did before and return the union ReturnType. Then, in the callee function, you add .<membername> so that you can access the apstirng value of it or the float/int/whatever value of it. Or you could use a struct or a class which gets a bit more complicated because you want to overload operators (which you can also do in a union).

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by Trauts
    once again, he only wants to return ONE data type, not 3. Its just that the data type that needs to be returned is different based on the number inside the [ ]s. If it is a 1, then it returns A apstring, otherwise it returns A float.
    Once again, my code suggests that s/he returns only one data type, a foo struct.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    its in a class.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Yes, it is in a struct, but it is considered to be 1 data type by the compiler. Hence, it can be returned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM

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