casting _to_ a class/struct

R

.rhavin grobert

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;)
 
A

Andre Kostur

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.
 
V

Victor Bazarov

..rhavin grobert said:
[..]
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
 
S

Sylvester Hesp

Andre Kostur said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top