cast operator for built in to class?

Discussion in 'C++' started by Gernot Frisch, Aug 4, 2004.

  1. Hi,

    I can define a class C that can handle a const char* and provide an
    operator:
    C::eek:perator string() const;

    can I define such a cast operator for const char* instead of class C??


    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
    Gernot Frisch, Aug 4, 2004
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    > Hi,
    >
    > I can define a class C that can handle a const char* and provide an
    > operator:
    > C::eek:perator string() const;
    >
    > can I define such a cast operator for const char* instead of class C??
    >
    >


    Surely you can just define a (non-explicit) constructor for C that takes
    a const char* as its argument... Then something like
    C c;
    c = "hello world";
    would create a nameless temporary using C::C(const char*) and copy to
    'c' using C::eek:perator=(const C&).

    Jacques.
    Jacques Labuschagne, Aug 4, 2004
    #2
    1. Advertising

  3. "Jacques Labuschagne" <> schrieb im Newsbeitrag
    news:Y53Qc.1746$...
    > Gernot Frisch wrote:
    > > Hi,
    > >
    > > I can define a class C that can handle a const char* and provide

    an
    > > operator:
    > > C::eek:perator string() const;
    > >
    > > can I define such a cast operator for const char* instead of class

    C??

    ^^^^^^^^^^^^^^^^^^
    > >
    > >

    >
    > Surely you can just define a (non-explicit) constructor for C that

    takes
    > a const char* as its argument... Then something like
    > C c;
    > c = "hello world";
    > would create a nameless temporary using C::C(const char*) and copy

    to
    > 'c' using C::eek:perator=(const C&).


    That would mean I have to define class "C", but I just want to have
    class "string"

    -Gernot
    Gernot Frisch, Aug 4, 2004
    #3
  4. Gernot Frisch wrote:
    >
    > Hi,
    >
    > I can define a class C that can handle a const char* and provide an
    > operator:
    > C::eek:perator string() const;
    >
    > can I define such a cast operator for const char* instead of class C??


    If I understand you, you want something like this

    operator const char*( string txt )
    {
    return txt.c_str();
    }

    The answer is: No. Conversion operators have to be a member of a class.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 4, 2004
    #4
  5. "Karl Heinz Buchegger" <> schrieb im Newsbeitrag
    news:...
    > Gernot Frisch wrote:
    > >
    > > Hi,
    > >
    > > I can define a class C that can handle a const char* and provide

    an
    > > operator:
    > > C::eek:perator string() const;
    > >
    > > can I define such a cast operator for const char* instead of class

    C??
    >
    > If I understand you, you want something like this
    >
    > operator const char*( string txt )
    > {
    > return txt.c_str();
    > }
    >
    > The answer is: No. Conversion operators have to be a member of a

    class.

    Since the above provides an obvoisly unambigious operator of which the
    usage is totally clear to everybody - why doesn't standart C++ allow
    it?

    Anyway - thank you, I can deal with it with a wrapper class around it.
    Gernot Frisch, Aug 4, 2004
    #5
  6. Gernot Frisch wrote:
    >
    > "Karl Heinz Buchegger" <> schrieb im Newsbeitrag
    > news:...
    > > Gernot Frisch wrote:
    > > >
    > > > Hi,
    > > >
    > > > I can define a class C that can handle a const char* and provide

    > an
    > > > operator:
    > > > C::eek:perator string() const;
    > > >
    > > > can I define such a cast operator for const char* instead of class

    > C??
    > >
    > > If I understand you, you want something like this
    > >
    > > operator const char*( string txt )
    > > {
    > > return txt.c_str();
    > > }
    > >
    > > The answer is: No. Conversion operators have to be a member of a

    > class.
    >
    > Since the above provides an obvoisly unambigious operator of which the
    > usage is totally clear to everybody - why doesn't standart C++ allow
    > it?


    Don't know.
    You have to ask the guys down at comp.std.c++ why this is so.
    There all the guys defining the standard hang around and discuss
    what changes should be done to the language.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 4, 2004
    #6
    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. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,062
    Dan Pop
    Feb 10, 2004
  2. EvilRix
    Replies:
    8
    Views:
    623
    Martin Dickopp
    Feb 14, 2004
  3. John Goche
    Replies:
    2
    Views:
    338
    Frederick Gotham
    Sep 4, 2006
  4. Pavel
    Replies:
    7
    Views:
    515
    Pavel
    Sep 19, 2010
  5. GF
    Replies:
    7
    Views:
    356
    Jorgen Grahn
    Oct 2, 2012
Loading...

Share This Page