Changing a variables data type

E

Eternally

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!
 
E

Eternally

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!
 
G

Gary Labowitz

Eternally said:
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.
 
E

Etno

Gary Labowitz said:
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?
 
K

Karl Heinz Buchegger

Etno said:
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 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;
 
C

Chris Mantoulidis

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.
 

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

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top