Automatic type conversion.

Discussion in 'C++' started by Jason Heyes, Nov 28, 2004.

  1. Jason Heyes

    Jason Heyes Guest

    I want Foo-type objects to be convertible to Bar-type objects. Can I use
    operator Bar() to do this just as I would use operator bool() to convert to
    boolean? Example:

    class Foo
    {
    public:
    operator Bar() const;
    };

    Thanks.
     
    Jason Heyes, Nov 28, 2004
    #1
    1. Advertising

  2. * Jason Heyes:
    > I want Foo-type objects to be convertible to Bar-type objects. Can I use
    > operator Bar() to do this just as I would use operator bool() to convert to
    > boolean?


    Yes.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 28, 2004
    #2
    1. Advertising

  3. Jason Heyes

    Rolf Magnus Guest

    Jason Heyes wrote:

    > I want Foo-type objects to be convertible to Bar-type objects. Can I use
    > operator Bar() to do this just as I would use operator bool() to convert
    > to boolean? Example:
    >
    > class Foo
    > {
    > public:
    > operator Bar() const;
    > };


    Yes, you can use that. But it is usually better to make a conversion
    constructor in Bar, like:

    class Bar
    {
    public:
    Bar(const Foo& rhs);
    };
     
    Rolf Magnus, Nov 28, 2004
    #3
  4. > I want Foo-type objects to be convertible to Bar-type objects. Can I use
    > operator Bar() to do this just as I would use operator bool() to convert to
    > boolean? Example:
    >
    > class Foo
    > {
    > public:
    > operator Bar() const;
    > };


    (mmm.. why not just try it?)

    Yes you can, but be aware that automatic conversions have their pitfalls
    and you may get some surprising results. Automatic conversions reduces
    the number of places a compiler can warn against potential logic errors.


    Jonathan
     
    Jonathan Mcdougall, Nov 28, 2004
    #4
  5. "Rolf Magnus" <> skrev i en meddelelse
    news:cocpvt$mgc$04$-online.com...
    > Jason Heyes wrote:
    >
    >> I want Foo-type objects to be convertible to Bar-type objects. Can I use
    >> operator Bar() to do this just as I would use operator bool() to convert
    >> to boolean? Example:
    >>
    >> class Foo
    >> {
    >> public:
    >> operator Bar() const;
    >> };

    >
    > Yes, you can use that. But it is usually better to make a conversion
    > constructor in Bar, like:
    >
    > class Bar
    > {
    > public:
    > Bar(const Foo& rhs);
    > };
    >

    .... and even better to make it explicit (if this is compatible with Jasons
    goal):

    explicit Bar(const Foo& rhs);

    /Peter
     
    Peter Koch Larsen, Nov 29, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Szabolcs
    Replies:
    3
    Views:
    733
    Gianni Mariani
    Jun 11, 2007
  2. Russ
    Replies:
    3
    Views:
    303
    Dan Bishop
    Jul 27, 2007
  3. MWimmer
    Replies:
    0
    Views:
    295
    MWimmer
    Sep 27, 2007
  4. Rahul

    automatic type conversion

    Rahul, Dec 11, 2007, in forum: C++
    Replies:
    6
    Views:
    363
    James Kanze
    Dec 12, 2007
  5. Bruce Eckel

    Automatic Type Conversion to String

    Bruce Eckel, Feb 13, 2012, in forum: Python
    Replies:
    6
    Views:
    187
    Ned Deily
    Feb 15, 2012
Loading...

Share This Page