casting _to_ a class/struct

Discussion in 'C++' started by .rhavin grobert, Jan 26, 2007.

  1. Hi there;-)

    I have the following struct:

    struct foo {
    union {
    unsigned __int64 ui64;
    char[8] c;
    };
    };

    what i now want to make possible is...

    foo a;
    foo b;
    int c;
    foo d;
    a = "abcdefg";
    b = 12;
    d = (foo) c;

    so i think i need to globally overload the cast operator, isn't it?

    i know how to overload the cast-operator to convert from my struct to
    $type, but how do i do it the other way 'round?

    thanx for your help,

    -.rhavin;)
     
    .rhavin grobert, Jan 26, 2007
    #1
    1. Advertising

  2. .rhavin grobert

    Andre Kostur Guest

    ".rhavin grobert" <> wrote in news:1169841744.091698.6020
    @s48g2000cws.googlegroups.com:

    > Hi there;-)
    >
    > I have the following struct:
    >
    > struct foo {
    > union {
    > unsigned __int64 ui64;
    > char[8] c;
    > };
    > };
    >
    > what i now want to make possible is...
    >
    > foo a;
    > foo b;
    > int c;
    > foo d;
    > a = "abcdefg";
    > b = 12;
    > d = (foo) c;
    >
    > so i think i need to globally overload the cast operator, isn't it?


    Why not assignment operators?

    foo & foo::eek:perator=(__int64 newvalue)
    {
    ui64 = newvalue;
    }

    foo & foo::eek:perator=(const char * cp)
    {
    strncpy(c, cp, sizeof(c));
    }

    > i know how to overload the cast-operator to convert from my struct to
    > $type, but how do i do it the other way 'round?


    That would require you to overload operators on int.... can't be done.
     
    Andre Kostur, Jan 26, 2007
    #2
    1. Advertising

  3. ..rhavin grobert wrote:
    > [..]
    > i know how to overload the cast-operator to convert from my struct to
    > $type, but how do i do it the other way 'round?


    I think you're talking about a parameterised constructor here.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 26, 2007
    #3
  4. "Andre Kostur" <> wrote in message
    news:Xns98C47E9C29755nntpspamkosutrnet@209.135.99.21...
    > ".rhavin grobert" <> wrote in news:1169841744.091698.6020
    >
    > Why not assignment operators?


    Because that won't solve the '(foo)c' piece of code ;). Constructors are the
    way to go (best in combination with assignment operators, but the latter
    won't be needed to make it all work).

    - Sylvester
     
    Sylvester Hesp, Jan 29, 2007
    #4
    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. kevin
    Replies:
    11
    Views:
    5,842
    Andrew McDonagh
    Jan 8, 2005
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,441
    Chris Fogelklou
    Apr 20, 2004
  3. Leif  Gruenwoldt

    casting a struct to a class

    Leif Gruenwoldt, Apr 6, 2006, in forum: C++
    Replies:
    23
    Views:
    806
    Jakob Bieling
    Apr 8, 2006
  4. Wally Barnes
    Replies:
    3
    Views:
    545
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    596
    James Kanze
    Dec 20, 2009
Loading...

Share This Page