return type of conversion opeator

This is a discussion on return type of conversion opeator within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, In a class T, if there is a conversion opeator G, we always define it like this, operator ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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,
    George

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about trying?
    Code:
    #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';
        std::cin.get();
    }
    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
    Location
    Toronto, ON
    Posts
    3,545
    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
    Posts
    6,290
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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, 10: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 Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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