473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

casting _to_ a class/struct

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

Jan 26 '07 #1
3 1572
".rhavin grobert" <cl***@yahoo.dewrote 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::operator=(__int64 newvalue)
{
ui64 = newvalue;
}

foo & foo::operator=(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.
Jan 26 '07 #2
..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
Jan 26 '07 #3

"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99. 21...
".rhavin grobert" <cl***@yahoo.dewrote 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
Jan 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cgacc20 | last post by:
I have a c struct from old code that cannot be modified and I am trying to write a wrapper C++ class around it. This class is often passed as a pointer to some c functions of a library and I...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.