I want to implement a 2D mesh class which is memory efficient, since it will be used in a Monte Carlo simulation. At this stage however, this is actually the same as a 2D array class. Needless to say, I am new to C++. I have written the following code.
#define MESH2D_H_INCLUDED
class Mesh2D
{
private:
double* mesh;
size_t rows;
size_t columns;
public:
/* constructor */
Mesh2D(size_t Nx, size_t Ny){
rows = Nx;
columns = Ny;
mesh = new double[Nx*Ny] {};
std::cout << "Mesh created." << std::endl;
}
/* destructor */
~Mesh2D()
{
delete[] mesh;
std::cout << "Mesh deleted." << std::endl;
}
/* accessors */
double getrows() const {return rows;}
double getcolumns() const {return columns;}
double& operator()(size_t i, size_t j)
{
if (i > rows || j > columns){
throw std::out_of_range("Index exceeds array bounds.");
}
return mesh[j + i*columns]; // row major access
}
/* copy constructor */
Mesh2D& operator=(const Mesh2D& rhs) // what is the difference between this line and the following? inline operator=(const Mesh2D& rhs)
{
if (rhs.rows != rows || rhs.columns != columns){
throw std::out_of_range("Assignment cannot be performed, mesh dimensions do not agree.");
}
mesh = new double [rows*columns];
// I want to avoid using a for loop
// for (int i=0; i<rows*columns; i++){
// mesh[i] = rhs.mesh[i];
// }
// Use this instead
memcpy(mesh, rhs.mesh, sizeof(rhs)); //however the output is not the expected.
std::cout << "copied!" << std::endl;
}
};
#endif // MESH2D_H_INCLUDED
//MAIN FUNCTION
#include <iostream>
#include "Mesh2D.h"
void PrintMesh2D(Mesh2D &mesh){ //why isn't it going to work if I add const? I mean: void PrintMesh2D(const Mesh2D &mesh)
for (int i=0; i<mesh.getrows(); i++){
for (int j=0; j<mesh.getcolumns(); j++){
std::cout << mesh(i,j) << " ";
}
std::cout << std::endl;
}
}
int main()
{
Mesh2D mesh{3,3};
Mesh2D newmesh{3,3};
for (int i=0; i<mesh.getrows(); i++){
for (int j=0; j<mesh.getcolumns(); j++){
mesh(i,j) = j + i * mesh.getcolumns();
}
}
newmesh = mesh;
PrintMesh2D(newmesh);
}
My questions are written as comments but I am listing them here as well:
Inside the copy constructor, what is the difference between this line
Mesh2D& operator=(const Mesh2D& rhs)
and this lineinline operator=(const Mesh2D& rhs)
?Again, inside the copy constructor I would like to avoid a for loop and use memcpy instead. However, the output is not the expected, what am I doing wrong?
Inside the main function: Why is this not going to compile?
void PrintMesh2D(const Mesh2D &mesh)
Finally, is the implementation complete? I mean, are there any important features/functions that are missing?
You are welcome to give me any piece of advice. Since I am new to C++, I have the feeling that I am writing bad buggy code.
EDIT:
I wrote the following as a copy constructor.
/* copy constructor */
Mesh2D (const Mesh2D& rhs)
{
rows = rhs.rows;
columns = rhs.columns;
mesh = new double[rows*columns];
memcpy(mesh,rhs.mesh,rows*columns*sizeof(double));
std::cout << "Copied!" << std::endl;
}
Looks right?