0

I'm currently on the second exercise of Chapter 7 in the Programming for Games Module 1/2 pdf. I'm completely lost on how to implement two of the functions. Those two functions are: The third constructor (creating an array from another array) and then the overloaded = operator.

I tried writing something for both, but failed with both. I believe I implemented everything else just fine (as the exercise is to implement the functions from the class blueprint), and I'm just not sure how to go about implementing those two. Help? And if you give me a solution, please explain why it is a solution.

class FloatArray {
    public:

        //Creates a float array with 0 elements
        FloatArray();

    //Creates a float array with 'size' elements
    FloatArray(int size);

    //Create a float array from another float array. Avoid memory leaks
    FloatArray(const FloatArray & rhs);

    //Frees up dynamic memory
    ~FloatArray();

    //Defines how a float array shall be assigned to another float array. No memory leaks
    FloatArray & operator = (const FloatArray & rhs);

    //Resize a float array to another size
    void resize(int newSize);

    //Returns the number of elements in an array
    int size();

    //Allow client to access the elements of FloatArray objects
    float & operator[](int i);

    private:
        float * mData; //Pointer to an array of floats (dynamic memory)
    int mSize; //The number of elements in an array
};

FloatArray::FloatArray() {
    mData = new float[0];
}

FloatArray::FloatArray(int size) {
    mData = new float[size];
}

FloatArray::FloatArray(const FloatArray & rhs) {
    const FloatArray * mData = & rhs;
}

FloatArray::~FloatArray() {
    delete[] mData;
}

FloatArray & FloatArray::operator = (const FloatArray & rhs) {

}

void FloatArray::resize(int newSize) {
    mSize = newSize;
}

int FloatArray::size() {
    return mSize;
}

float & FloatArray::operator[](int i) {
    return mData[i];
}

void PrintFloatArray(FloatArray & fa) {
    std::cout << "{ ";
    for (int i = 0; i < fa.size(); ++i)
        std::cout << fa[i] << " ";
    std::cout << "}" << endl << endl;
}

int main() {
    FloatArray A;

    A.resize(4);
    A[0] = 1.0 f;
    A[1] = 2.0 f;
    A[2] = 3.0 f;
    A[3] = 4.0 f;

    std::cout << "Printing A. . .";
    PrintFloatArray(A);

    FloatArray B(A);

    std::cout << "Printing B. . .";
    PrintFloatArray(B);

    /* FloatArray C = A;

    std::cout << "Printing C. . ." ;
    PrintFloatArray(C);

    A = A = A = A;

    std::cout << "Printing A. . ." ;
    PrintFloatArray(A);*/

    return 0;
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
  • You question headline look like a 5 line example. Can you please reauce your example code to a *minimal* example which is needed to understand the problem! Hopefully for future posts! – Klaus Mar 14 '19 at 08:16
  • `const FloatArray * mData = & rhs;` - this doesn't do what you think it does. Also, you have a buffer overflow error because your `resize` doesn't actually allocate any new memory. – Raymond Chen Mar 14 '19 at 14:08
  • So the program works as intended now and I would never have guessed there's a buffer overflow error. Can you tell me how to spot those? As in, is there a way/tool (beyond looking or knowing about the code) to detect those? – TheTypicalGoon Mar 14 '19 at 18:09

2 Answers2

1

In the copy constructor, you have to:

  1. Set the value of mSize to be the same as the object you are creating a copy from.

  2. Make sure to allocate memory for the the mData member of the object.

  3. Copy each element of the array from the object you are creating a copy from.

FloatArray::FloatArray(const FloatArray &rhs) : mSize(rhs.mSize)
{
   mData = = new float[mSize];
   for ( int i = 0; i < mSize; ++i )
   {
      mData[i] = rhs.mData[i[;
   }
}

For the assignment operator, it will be better to use the copy-and-swap idiom.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks for the detailed reply! And it worked! (though there were a few tiny corrections I had to make) I do have one question though... I've never seen anything like : mSize(rhs.mSize). What exactly is that piece of code there doing? – TheTypicalGoon Mar 14 '19 at 07:03
  • You need to find a better C++ book. The parenthesis notation for member initialization is pretty fundamental. https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – Raymond Chen Mar 14 '19 at 14:10
  • @TheTypicalGoon, see https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor. – R Sahu Mar 14 '19 at 15:33
0

The third constructor (creating an array from another array)

your constructor doesnt create array from another array, its just creating a local pointer that points to the FloatArray that you pass, at this point theres still only 1 array that exist. and you have memory leak there by not freeing the pointer before going out of scope.

if you just want to copy your object then you can just do shallow copying by just deleting your copy constructor (your third constructor) and it will do it for you automatically.

Lorence Hernandez
  • 1,189
  • 12
  • 23