CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Posts
    5

    new to C++ need some help with dynamic array

    I'm trying to make an Dynamic Array of an Object.

    but the first step I want to make sure I have everything correct creating my Dynamic Array.

    Here is what I have for my .h file.

    Code:
    class course{
        Public:
            course(int maxSize = 1024);
            ~course();
       Private:
            int mySize;
            int maxCapacity;
            string * myArray;
    And here is what I have on my .cpp file
    Code:
    (All the include)...
    course::course(int maxSize):mySize(0), myCapacity(maxSize){
         myArray = new(nothrow) string[maxSize];
    }
    course::~course(){
        delete [] myArray;
    }
    is that correct? I still need to create the copy constructor too I'm working on that now.

    but what I want to know is. Once I have my Dynamic Array how can I make it an array of another class that I have.
    }
    Last edited by Ennio; February 20th, 2008 at 11:27 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: new to C++ need some help with dynamic array

    Quote Originally Posted by Ennio
    I'm trying to make an Dynamic Array of an Object.
    If this is not a classroom exercise, I suggest you use std::vector.
    but the first step I want to make sure I have everything correct creating my Dynamic Array.

    Here is what I have for my .h file.

    Code:
    class course{
        Public:
            course();
            ~course();
       Private:
            int maxSize;
            string * myArray;
    That cannot be your real code. There is no such keyword as Private or Public in C++. The keywords are private and public.

    Next time, please post your real code, i.e. copy and paste it directly from your code editor into the message window so as to avoid any typographical errors from interfering with the problem/solution.
    And here is what I have on my .cpp file
    Code:
    (All the include)...
    course::course(){
         myArray = new(nothrow) string[10];
    }
    course::~course(){
        delete [] myArray;
    }
    is that correct?
    I guess so, not knowing what else is in your class.
    I still need to create the copy constructor too I'm working on that now.
    You also need an assignment operator. However, usage of std::vector requires you to not write any copy constructor or assignment operator:
    Code:
    #include <vector>
    #include <string>
    class course
    {
        public:
            course();
    
       private:
            int maxSize;
            std::vector<std::string> myArray;
    };
    You don't even need a destructor anymore.

    Regards,

    Paul McKenzie

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