-2

I am learning dynamic memory allocation. I have the following class where 'class A' should own a dynamically allocated array in the constructor. Also the copy constructor and destructor should be modified. This is what I have so far...

#include <iostream>
#ifndef A_HH
#define A_HH

#include "B.hh"

class A {
public:

  A() {  B *array = new B[12];}
  A(const A&) { /* Do not know what to put here..*/ }
  ~A() { delete[] array;}

private:

   //B array[12] ; <- This is the array that I have to modify so it becomes dynamic. 
   B *array;
} ;

#endif 
Derlin
  • 9,572
  • 2
  • 32
  • 53
Rob Schneider
  • 165
  • 5
  • 16
  • As an aside, you should put the `#include ` inside the header guard. – LiamT Mar 24 '17 at 10:04
  • You should get used to the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) (or since c++11, [rule of five](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11)). Additionally, you might have a look at the [copy and swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). – Aconcagua Mar 24 '17 at 10:04

2 Answers2

1

For starters your default constructor there is overriding the member variable "array" with a local variable of the same type, so you want the default constructor to look like this:

A() { array = new B[12]; }

Then the copy constructor presumably needs to deep copy the array over, but with a simple array you can't tell the arraysize at runtime. You need to either move to a smarter container (e.g. stl::vector) or store the size, but a naive copy constructor would look like this:

A(const A& other)
{
   array = new B[12];
   for(int i=0;i<12;i++)
   {
       array[i] = other.array[i];
   }
}
Iain Brown
  • 1,079
  • 3
  • 11
  • 23
  • 1
    Hardcoding the array size to 12 is a really bad idea, by the way, at least add a #define or static const int somewhere. – Iain Brown Mar 24 '17 at 10:00
  • What do you mean with "add a #define or static const int" exactly? I have very little experience with this kind of stuff. ;) – Rob Schneider Mar 24 '17 at 10:06
  • if your array is always 12 in size then within your class add a `static const int ARRAY_SIZE = 12;` Then whenever you want to put 12, put ARRAY_SIZE instead. If this array is of variable length then this is not wanted. – Iain Brown Mar 24 '17 at 10:10
  • *"is overriding the member variable"* in conventional terminology: "hides the member variable" – eerorika Mar 24 '17 at 10:17
  • With your input I have now changed 12 to 'len' and added 'int len' to 'private'. But when I compile, the program keeps calling to constructor and does not stop. How can I fix this? – Rob Schneider Mar 24 '17 at 10:17
-1

Check out this code:

#include <iostream>


using namespace std;

class MyArray{
public:
    int* array;
    int length;

    MyArray(int length){ // constructor which takes length of array as argument
        this->length = length;
        this->array = new int[this->length];

        for(int i=0; i<this->length; i++){
            this->array[i] = i;
        }
    }

    MyArray(const MyArray &obj){
        this->length = obj.length;
        this->array = new int[this->length];
        for(int i=0; i<this->length; i++)
        {
            this->array[i] = obj.array[i];
        }
    }

    ~MyArray(){ // destructor
        delete[] this->array;
    }


    void print(){ // test method for printing the array
    for(int i=0; i<this->length; i++){
            cout << this->array[i] << endl;
        }
        cout << endl;
    }

private:
protected:

};
int main()
{
    MyArray *array = new MyArray(10);
    MyArray *array2 = new MyArray(*array); // call copy constructor

    array->print();
    array2->print();

    delete array;
    delete array2;
}
Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24