Combining Functions with different return types

This is a discussion on Combining Functions with different return types within the C++ Programming forums, part of the General Programming Boards category; How in the world do I combine these functions into one? Do I make them a template? I can't find ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Exclamation Combining Functions with different return types

    How in the world do I combine these functions into one? Do I make them a template? I can't find anything similar out there. Please forward any help to <removed>

    Code:
    // Converts a String to an Integer
    int GetIntVal(string strConvert)
    {
        int intReturn;
        intReturn = atoi(strConvert.c_str());
    
        return intReturn;
    }
    
    // Converts a String to a Double (Float)
    double GetDoubleVal(string strConvert)
    {
        double doubleReturn;
        doubleReturn = atof(strConvert.c_str());
    
        return doubleReturn;
    }
    Last edited by laserlight; 12-27-2008 at 09:07 AM. Reason: Removed email address to protect against spam bots.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Since you asked on a forum, the answers should be given on the forum, not via email. You can enable email notification if you wish to be notified via email (I believe it is called thread subscription).

    As for the problem: why do you want to do that? I would suggest function overloading, but that is not possible since the function signatures are the same except for the name. You could turn them into a function template and then specialise the function template, but to call the correct version would mean having to explicitly provide the template argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    A template version may look like this:

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    template <typename T> T getvalue(const string & s)
    {
        T v;
        stringstream str;
    
        str << s;
        str >> v;
    
        return v;
    }
    
    int main()
    {
        string pi("3.141592654");
    
        cout << getvalue<int>(pi) << "\n"
             << getvalue<double>(pi) << endl;
    
        return 0;
    }
    But there are some problems with this function. It does not check for conversion errors, and I do not know how the strstream operations affect the variable's precision.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    If you really want to do that, then you might as well use boost::lexical_cast.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    It didn't even occur to me to use a strstream! That will work great! The precision is only there to 13 decimal places, but I don't need that kind of precision; THANKS!

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    template <typename T> T getvalue(const string & s)
    {
        T v;
        stringstream str;
        
        str << s;
        
        
        
        str >> v;
    
        return v;
    }
    
    int main()
    {
        string pi("3.141592653589793238462643383279502884197169399375");
        
        cout.precision(40);
        cout << getvalue<int>(pi) << "\n"
             << getvalue<double>(pi) << endl;
    
        system("PAUSE");
        return 0;
    }

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    FYI, it's not streams that have trouble with precision. It's the floating point types themselves, which represent approximations of real values. A double is not large enough to store pi to the 40th digit exactly.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

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