By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Changing a variables data type

P: n/a
Hey folks,

To me, this sounds like a crazy question, but I'll throw it out there
anyway.

Is it possible to change a variables data type half way through a running
program? If so, how?

You're probably thinking why would you ever want to do that. Valid
question. If you're curious why I'd like to do it, read on. But otherwise,
you can stop here and just answer if it's possible.

I've got an Expression Template program. As you probably know, an
expression variable has as it's data type, the data types of all the nodes
and leaves that make up it's parse tree.

So, the expression (x+y) + z would have a data type similar to the following
(depending on how you set up your program of course):
DExpr< DBinExprOp<DExpr< DBinExprOp<DExpr<int>, DExpr<int>, DApAdd> >,
DExpr<int>, DApAdd> >

What I'd like to do is merge two expression's parse trees into one and store
the newly merged parse tree in one of the original trees variables.
So if I have:
myExpr1((x+y) + z);
myExpr2(a + b);
myExpr1 = (myExpr1 + myExpr2);

In this example, the original myExpr1 would have the data type
DExpr< DBinExprOp<DExpr< DBinExprOp<DExpr<int>, DExpr<int>, DApAdd> >,
DExpr<int>, DApAdd> >

myExpr2 would have the data type
DExpr< DBinExprOp<DExpr<int>, DExpr<int>, DApAdd> >

And the merged tree would have the data type
DExpr< DBinExprOp< DExpr<DBinExprOp<DExpr< DBinExprOp<DExpr<int>,
DExpr<int>, DApAdd> >, DExpr<int>, DApAdd> >, DExpr<DBinExprOp<DExpr<int>,
DExpr<int>, DApAdd> > >

But, for this to work, it would essentially mean changing myExpr1's data
type from it's original to the new one above.

Impossible, right?

I was thinking if it were possible the only way would be to create
amyExprTemp which holds the new tree,

Thanks a lot!
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Sorry. Accidentally hit send. So I was saying:

I was thinking if it were possible the only way would be to create a
myExprTemp which holds the new tree, and then changing the address of
myExpr1 to the address of myExprTemp. But, I just doubt g++ would be able
to handle doing that.

If it can, that's great. I'd just be surprised....But if so, how would it
be done, cause so far I can't get something like that to compile?

Thanks!
Jul 22 '05 #2

P: n/a
"Eternally" <m@r.com> wrote in message
news:ba****************@twister.nyroc.rr.com...
Hey folks,

To me, this sounds like a crazy question, but I'll throw it out there
anyway.

Is it possible to change a variables data type half way through a running
program? If so, how?

You're probably thinking why would you ever want to do that. Valid
question. If you're curious why I'd like to do it, read on. But otherwise, you can stop here and just answer if it's possible.


If you mean using the coded bits of a variable as if they represented
another type, use reinterpret_cast.
If you mean convert the coded bits to a different type, just define a
different variable and convert to it.
I didn't bother reading the rest of your post. Too tired.
--
Gary
Jul 22 '05 #3

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:f-********************@comcast.com...
"Eternally" <m@r.com> wrote in message
news:ba****************@twister.nyroc.rr.com...
Hey folks,

To me, this sounds like a crazy question, but I'll throw it out there
anyway.

Is it possible to change a variables data type half way through a running program? If so, how?

You're probably thinking why would you ever want to do that. Valid
question. If you're curious why I'd like to do it, read on. But otherwise,
you can stop here and just answer if it's possible.


If you mean using the coded bits of a variable as if they represented
another type, use reinterpret_cast.


What I'm trying to do is the equivalent of taking a variable originally
declared as int, and then telling it that it is now a float. In addition, I
would be setting it's value to a float number, and expecting it to retain
that. For example
int x = 2;
//convert x into a float somehow
x=3.4;
cout << x << endl;

What gets printed out should be 3.4.
Possible with reinterpret_cast?
If you mean convert the coded bits to a different type, just define a
different variable and convert to it.


If I understand you correctly, that may be more what I'm trying to do. How
would that be done?
Jul 22 '05 #4

P: n/a
Etno wrote:

What I'm trying to do is the equivalent of taking a variable originally
declared as int, and then telling it that it is now a float. In addition, I
would be setting it's value to a float number, and expecting it to retain
that. For example
int x = 2;
//convert x into a float somehow
x=3.4;
cout << x << endl;

What gets printed out should be 3.4.
Possible with reinterpret_cast?
No. You declared x to be int. Period. End of story.
If you mean convert the coded bits to a different type, just define a
different variable and convert to it.


If I understand you correctly, that may be more what I'm trying to do. How
would that be done?


int x = 2;
double y;

y = x;

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

P: n/a
or you can just do (newtype)var_of_some_other_type which is mostly
C-ish, but since we're talking C++ here, I'd say what the other guy
said...

Plus, if you're making a parser, note that there's a much easier way
to do it that make pointers to trees and such stuff... e-mail me or
something if you're interested.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.