class Element {
public:
..
virtualunsigned NumberOfNodes() = 0;
..
};
class Triangle : public Element {
public:
..
virtualunsigned NumberOfNodes() { return 3; }
..
};
class Mesh {
public:
..
unsigned NumberOfNodes() { return elem->NumberOfNodes()*number_of_elements; }
..
private:
..
Element *elem;
unsigned number_of_elements;
..
};
Is it possible to implement this better? All the element stuff can be static, but this is not possible with the abstract class. I want to have Mesh independent of a specific element. With the code above, if I have multiple meshes I have one instance of an element, e.g., Triangle for each mesh. Although they are all exactly the same. Have you any idea?
I have one instance of an element, e.g., Triangle for each mesh
Share a single triangle between all Meshes. Like Flyweight pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct Prototypes //More robust in regards to ODR, should make sure that it is not instantiable, extra work
{
staticconst Triangle triangle {};
//...
}
//Or
namespace Prototypes //Needs to be in implementation file and have a header counterpart
{
const Triangle triangle;
//...
}
//Make elem pointer to constant
//const Element* elem;
Mesh::Mesh() : elem(& Prototypes::triangle;)
{}