Thread: return type of conversion opeator

  1. #1
    Registered User
    Join Date
    May 2006

    return type of conversion opeator

    Hello everyone,

    In a class T, if there is a conversion opeator G, we always define it like this,

    operator G()

    without return type. So the implicit return type is G or G&? I can not find it in the C++ programming language book. :-)

    thanks in advance,

  2. #2
    The larch
    Join Date
    May 2006
    How about trying?
    #include <iostream>
    struct A
        int n;
        operator int() 
            std::cout << "Conversion to int\n";
            return n; 
    int main()
        A a;
        a.n = 100;
        //error: invalid initialization of reference of type 'int&' from expression of type 'A'
        //int& s = a; 
        int t = a;
        t = 42;
        std::cout << a.n << ' ' << t << '\n';
    The error would go away if you provided an operator int& but then the initialization of t would become ambigous.
    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).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    I would avoid creating custom conversion operators all together. It's too easy for them to get called when you don't intend for them to be called. Why not provide an explicit function to convert to a new type, kind of like std::string::c_str().

  4. #4
    Registered User
    Join Date
    Jun 2005
    To answer the original question, operator G() has an implicit type of G.

    To respond to cpjust, I agree the problem of unwanted implicit conversions can be a significant one, but disagree with that being a reason to avoid custom conversions altogether. With sensible design, it is possible to identify cases where implicit conversions are desirable (eg implicit conversions between very high precision floating point and very large integral types, should those be implemented class types).

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    I agree with grumpy. Some things are safe - mostly returning const of some type. But when you need a non-const, you should explicitly call a member function to attain it. This applies especially to classes that wrap around buffers and memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM