-
April 27th, 2006, 08:00 AM
#1
how to define a type at run time
Hi,guys,
there are two data frames,the first gives the defining data of the second,so I thought it would define data type in the second frame according to the first frame at run time?
how can I resolve it? I have been puzzled long time...too hard to me, anyone could help me...thx!
-
April 27th, 2006, 08:47 AM
#2
Re: how to define a type at run time
Can't undestand what you want. Can you give a real example?
-
April 27th, 2006, 08:49 AM
#3
Re: how to define a type at run time
You cannot define a new type at runtime.
What you might want to do is dynamically create an existing type at runtime based on runtime information.
You'll also need to determine what you are going to do with the result when you have it.
You should, in this case, use an abstract class factory which will create the appropriate class and then you call the appropriate method on it (which must also be abstract).
You can create adapters for existing classes that implement your interfaces with the concrete classes that already exist.
-
April 28th, 2006, 12:50 AM
#4
Re: how to define a type at run time
thanks for your advice,could you give me some articles or sample codes about your idea? Are there any cool links or forums about design pattern?I just know something about design pattern...but I am a novice about using abstract class factory pattern...thx
Originally Posted by NMTop40
You cannot define a new type at runtime.
What you might want to do is dynamically create an existing type at runtime based on runtime information.
You'll also need to determine what you are going to do with the result when you have it.
You should, in this case, use an abstract class factory which will create the appropriate class and then you call the appropriate method on it (which must also be abstract).
You can create adapters for existing classes that implement your interfaces with the concrete classes that already exist.
Last edited by ephemera; April 28th, 2006 at 01:02 AM.
-
April 28th, 2006, 01:39 AM
#5
Re: how to define a type at run time
i think u r looking for something like this have a look for void pointer.
Code:
void *pv; // Okay
int *pint; int i;
int main() // main has no return value
{
pv = &i;
// Cast optional in C required in C++
pint = (int *)pv;
}
-
April 28th, 2006, 02:22 AM
#6
Re: how to define a type at run time
Code:
int *frame;
void *frameSecond;
int *IntSecFrameData;
char *CharSecFrameData;
float *FloatSecFrameData;
frame=getFrameFirst();
switch(*frame)
{
case IntData:
intSecFrameData=(int *)getFrameSecond();
break;
case CharData:
charSecFrameData=(char *)getFrameSecond();
break;
case FloatData:
FloatSecFrameData=(float *)getFrameSecond();
break;
default:
frameSecond=(void *)getFrameSecond();
break;
}
I think you looking ..for something like above stuff.........
-Anant
"Devise the simplest possible solution that solves the problems"
-
April 28th, 2006, 02:34 AM
#7
Re: how to define a type at run time
RTTI'll help you!
typeid( your_variable ).name()
one little problem - different compilers has different types of name strings
for example, int - "i", "int", "integer" etc. for different C++ compilers. Check your compiler's names before using.
..but void*... I don't know, how it'll work after casting to void and backwards. But it gives dynamic type of variable, but not for referenced type.
!and!
you must always remember!
if you want to use dynamic type identification, it means, that you make an error in design time. 90% of using RTTI - result of such errors.
For example, you can use virtual functions instead of RTTI.
Last edited by BugHunter; April 28th, 2006 at 02:43 AM.
make it run, make it right, make it fast, make it small
(copyright Kent Beck)
-
April 28th, 2006, 10:58 PM
#8
Re: how to define a type at run time
thx...but how to define data struct at run time? The type of data wasnt defined utill at run time...it seemed that the type of data was defined at compile-time in your code
Originally Posted by humptydumpty
i think u r looking for something like this have a look for void pointer.
Code:
void *pv; // Okay
int *pint; int i;
int main() // main has no return value
{
pv = &i;
// Cast optional in C required in C++
pint = (int *)pv;
}
-
April 28th, 2006, 11:52 PM
#9
Re: how to define a type at run time
thx for your advice,your advice is good,but my second frame is not fixed...maybe it includes two char,three int..maybe it includes other data type... data type in the second frame was defined at run time in my problem...maybe NMTop40's advice help me more...I wanna something like abract factory class pattern...but I am still puzzled...
Originally Posted by anantwakode
Code:
int *frame;
void *frameSecond;
int *IntSecFrameData;
char *CharSecFrameData;
float *FloatSecFrameData;
frame=getFrameFirst();
switch(*frame)
{
case IntData:
intSecFrameData=(int *)getFrameSecond();
break;
case CharData:
charSecFrameData=(char *)getFrameSecond();
break;
case FloatData:
FloatSecFrameData=(float *)getFrameSecond();
break;
default:
frameSecond=(void *)getFrameSecond();
break;
}
I think you looking ..for something like above stuff.........
-Anant
Last edited by ephemera; April 28th, 2006 at 11:55 PM.
-
April 29th, 2006, 06:25 AM
#10
Re: how to define a type at run time
Originally Posted by NMTop40
You cannot define a new type at runtime.
What you might want to do is dynamically create an existing type at runtime based on runtime information.
You'll also need to determine what you are going to do with the result when you have it.
You should, in this case, use an abstract class factory which will create the appropriate class and then you call the appropriate method on it (which must also be abstract).
You can create adapters for existing classes that implement your interfaces with the concrete classes that already exist.
can u explain in detail about the abstract factory class with a code as an example or some reference material if possible
-
May 2nd, 2006, 03:45 AM
#11
Re: how to define a type at run time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|