Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composition in c++ with raw or smart pointer?

A little example of what I want to do.

I have a list of (stack allocated) vertices

class Vertex {

    int id;
    double x;
    double y;
    double z;
};

and want to create a list of edges

class Edge {

    int id;
    Vertex * source;
    Vertex * target;
};

with two pointers to its source and target vertex.

Normally I would go for a reference here, but I want to be able to change the source or target vertex during runtime, so I need some kind of pointer type.

So my question: is there a smart pointer which would be useful here or should I just use a normal pointer as above?

Edit

Addressing some points, which came up in the answers:

First, the list should own the vertices, which is why they are on the stack.

Second, the ids are for an other program.

It needs a file with a list of all vertices and their coordinates, and a list of all edges and the ids of its two vertices.

Third, I need some kind of pointer, because the ids of the vertices change during runtime and the source and target vertex of a edge could change to.

(amongst other things some kind of cuts and slicing is performed)

like image 202
randooom Avatar asked Dec 11 '10 14:12

randooom


2 Answers

What Composition is


Composition (in UML terms) is an association when a given object is "a part" of another objects- i.e. it has the same lifetime and - what's most important and characteristic - does not exist / make sense on its own.

According to this description, composition is not what we want to achieve - see the second part.

In C or C++, the best way to implement composition is without using any pointers:

class Edge {

    int id;
    Vertex source;
    Vertex target;
};

This approach is best in terms of memory usage (one memory block for the whole object along with composite objects) and probably efficiency too. When you need composition - go for this solution.

Why Composition isn't suitable for this problem


Composition implies some consequnces:

  • The composed objects don't exist on their own,
  • Their association with the composite object is permanent during that object's lifetime.

In your data model, you have separate:

  • Array of vertices (independent),
  • Array of edges.

Both possibly stack-allocated (but that's not really important).

Then you want each edge to refer to N vertices.

The edge does NOT own them - it only does refer to them. So neither composition, nor a smart pointer (which is designed to introduce some kind of ownership association) is not what you want here, because the design says that the vertices are owned by the array of vertices, not by the edges.

So go for a plain pointer.

You could even use array indices instead of pointers as an alternative (which indeed has its uses, e.g. if you'd want to use the latter array as an index buffer for 3D rendering). All depends on your needs.

like image 68
Kos Avatar answered Oct 20 '22 00:10

Kos


Generally speaking, smart pointers are "smart" because they deal with the ownership issues involved. In the above, who do you want to own the vertices?

like image 34
Stuart Golodetz Avatar answered Oct 19 '22 23:10

Stuart Golodetz