I have a function and a class as below
class Vertex {
public:
int mId;
public:
Vertex(int info=-1) : mId(info) {
}
};
class Edge {
public:
Vertex mStart, mEnd;
int mWeight;
public:
Edge(Vertex start=-1, Vertex end=-1, int wt=-1) :
mStart(start), mEnd(end), mWeight(wt) {
}
};
class Graph {
void addEdge(const Edge& e) {
//Adds this edge to a vector
}
};
shared_ptr<Graph> mygraph(new Graph(13 //no of vertices
, 17 //no of edges
, false));
mygraph->addEdge(Edge( 1, 2, 1));
mygraph->addEdge(Edge( 3, 1, 1));
mygraph->addEdge(Edge( 1, 6, 2));
mygraph->addEdge(Edge( 1, 7, 4));
...
Here I am passing direct Edge values in a constructor and get no crash. But I guess there will be a memory leak here. Whats the correct way to pass an object by reference after doing construction?
PS: Assume that Vertex is an implicit constructor accepting int as id.