Thread: constructor problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    20

    constructor problem

    As a part of an exercise I need to built Drawing class that contains an array
    of pointers to objects from other classes (different types of shapes)
    I have a few problems with the constructor.
    at first I tried:
    Code:
    typedef shape* pshape;
    ...
    
    Drawing(int n=10) :size_(n)
      {
    	drawing_ = new pshape[n];
    	for (int i=0; i<n; i++)
    	     drawing_[i] = new shape();
      }
    but after the line: drawing_ = new pshape[n]; the program returns to main()
    without entering the for loop;
    so I tried this instead:
    Code:
    Drawing(int n=10) :size_(n)
      {
    	pshape* temp;
           temp = new pshape[n];
           if (temp==NULL) exit(-1);
    	for (int i=0; i<n; i++)
            {
    	      temp = new shape();
                  if (temp[i]==NULL) exit(-1);
             }
           drawing_=temp;
      }
    and this time it returns to main right after pshape* temp;
    what wrong with the code?
    Last edited by stewie griffin; 01-17-2009 at 06:33 AM.

Popular pages Recent additions subscribe to a feed