nested struct using pointers c++ -


i newbie of c++. using c++11 standards , mingw64 understand nested struct.

but not able figure out wrong following program. trying use nested struct using pointers. not want use "new" keywords this. also, understand leaking memory in program.

#include<iostream> struct model{   int a;   double b;   struct shape {       int c ;       double d;   };   shape  *pshape;  };  int main(){   model *m ;   m->a = 2;   m->pshape->c=3;   delete m;   printf("done\n"); } 

please me in understanding wrong , best , clean way of using nested struct c++11.

in above program pointer "pshape" destroyed when program goes out of scope ?

regards, avi


thank comments. learning more guys learn book. comments informative humorous. thank guys.

based on suggestion here attempt. please let me know if think has flaws:

#include<iostream> using namespace std;  struct model{   int a;   double b;   struct shape {       int c ;       double d;    } *pshape =null;   shape sh;  };  int main(){    model m;   model *pm;   pm = &m;   pm->pshape=&pm->sh;    pm->a = 4;   pm->b = 3.24;   pm->pshape->c=101;      cout << pm->a << endl;   cout << pm->pshape->c << endl;   cout << pm->pshape->d << endl;    printf("done\n"); } 

here how had working

#include<iostream> using namespace std; struct model{   int a;   double b;   struct shape {       int c ;       double d;   }*pshape;  };  int main(){   model m;   model *pm;   pm = &m;   pm->a = 2;   pm->pshape->c=3;   printf("done\n"); } 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -