How to create objects dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chaitannya
    New Member
    • Aug 2006
    • 1

    How to create objects dynamically

    I have created a class CMyGraph, which plots points of a graph on screen. The code i am using right now is as follows.

    CMyGraph* plotPoint1 = new CMyGraph();
    plotPoint1->SetData(0, 64);

    But if i have to plot multiple points i do it hardcoded like.. ..
    CMyGraph* plotPoint1 = new CMyGraph();
    plotPoint1->SetData(0, 64);
    CMyGraph* plotPoint2 = new CMyGraph();
    plotPoint2->SetData(0, 64);
    CMyGraph* plotPoint3 = new CMyGraph();
    plotPoint3->SetData(0, 64);

    I intent to do this dynamically , i.e. the values will come from the database. And those many points will be plotted. In short those many objects will be created. I am a newbie .. can anyone suggest a solution for that
  • malik
    New Member
    • Aug 2006
    • 4

    #2
    You can create an array of objects that is pointed by a pointer like...

    CMyGraph* ptr = new CMyGraph[num];
    //where num = the number of objects you want to create at run time.

    then is a for loop take the points from the user at run time using the setdata function..

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Can't your write your CMyGraph class so that it can plot multiple points?

      But if i have to plot multiple points i do it hardcoded like.. ..

      CMyGraph* plotPoint1 = new CMyGraph();
      plotPoint1->SetData(0, 64);
      plotPoint1->SetData(0, 64);
      plotPoint1->SetData(0, 64);

      Comment

      Working...