return a random data type?

This is a discussion on return a random data type? within the C++ Programming forums, part of the General Programming Boards category; hello, I have that strange question about if a function can return a random type of data. Let me explain ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287

    return a random data type?

    hello, I have that strange question about if a function can return a random type of data. Let me explain with a sample
    Code:
    #include <conio.h>
    #include <stdio.h>
    
    #define SPEED   1
    #define NAME    2
    
    struct STSPEED {int s,t;};
    
    allDataTypes myStrangeFunction(int what)
    {
    switch(what)
        {
        case SPEED:
            {
            return (allDataTypes)STSPEED;
            }
        break;
        case NAME:
            {
            return (allDataTypes)char*;
            }
        break;
        }
    }
    
    float mySpeed(STSPEED sts)
    {
    return (float)(sts.s/sts.t);
    }
    
    int main()
    {
    allDataTypes ADT;
    
    /*first let me calcule an auto speed*/
    ADT=myStrangeFunction(SPEED);
    memset(ADT,0,sizeof(ADT));
    ADT.s=100;
    ADT.t=1;
    printf("%f\n",mySpeed(ADT));
    
    /*and now let me printf my name*/
    ADT=myStrangeFunction(NAME);
    ADT=(char*)malloc(256);
    memset(ADT,0,sizeof(ADT));
    sprintf(ADT,"My name");
    printf("%s\n",ADT);
    free(ADT);
    
    getch();
    return 0;
    }
    obviously that won't compile. my question is if that's possible to create a data type that can be converted to whatever other data type, so on my sample the variable ADT would be fantasiously converted first into a struct and last into a char*.
    is it possible to do?

    thank's in advance
    Niara

    PS: I haven't tryed to google for that, nor search on the cboard search engine, because I'm not sure about the title for so ridiculous question

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    void *,

    oh, you want to return the actual type.. maybe you can do this with a macro.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you could do something like this with a template function
    Code:
    template <typename RETURN_TYPE>
    RETURN_TYPE DoSomething(void)
    {
        RETURN_TYPE result = RETURN_TYPE(); // calls default ctor (works for pods too)
        return result;
    }
    not sure what you hope to achieve with this though...
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    hello, thank's both for your time
    yes, I'm also not sure for what this will be usefull, it was only something that I have always been thinking about but I never dare to ask.
    I have been thinking about the macro solution but I'm not sure about how to do it, also I have tried with the void* but I always get an error like 'can't convert type to void'. Then I have been looking for more info about templates, but I can't find a way to return some datatype without errors.
    tomorrow I'll continue testing all that.
    thank's.

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 12:29 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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