Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting _to_ a class/struct

Reply
Thread Tools

casting _to_ a class/struct

 
 
.rhavin grobert
Guest
Posts: n/a
 
      01-26-2007
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

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-26-2007
".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:perator=(__int64 newvalue)
{
ui64 = newvalue;
}

foo & foo: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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-26-2007
..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


 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-29-2007

"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
'STD_LOGIC_VECTOR ' to 'unsigned' type casting Ben Nguyen VHDL 6 09-20-2003 05:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57