CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2003
    Posts
    24

    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!

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: how to define a type at run time

    Can't undestand what you want. Can you give a real example?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.

  4. #4
    Join Date
    Nov 2003
    Posts
    24

    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

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

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

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

  6. #6
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up 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"

  7. #7
    Join Date
    Apr 2006
    Posts
    16

    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)

  8. #8
    Join Date
    Nov 2003
    Posts
    24

    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
    Quote 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;
    }

  9. #9
    Join Date
    Nov 2003
    Posts
    24

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

  10. #10
    Join Date
    Mar 2006
    Location
    India
    Posts
    72

    Re: how to define a type at run time

    Quote 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

  11. #11
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: how to define a type at run time

    For some basic sample code, look at http://www.earlpurple.com/CodeBase/Factory.h

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured