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;
}