Abstract "static" class and performance.

closed account (9267ko23)
Hi,

I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Element {
public:
 ..
 virtual unsigned NumberOfNodes() = 0;
 ..
};

class Triangle : public Element {
public:
 ..
 virtual unsigned 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
{
    static const 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;)
{}
Topic archived. No new replies allowed.