'How can I access class members after creating a new object for that class?

I know my question doesn't really explain my problem all that well, but I'll try my best here. I have a graph program. It should add new vertices and edges, and each one should have an id.

class Graph
{
private:
    std::vector<Vertex> vertices;
    std::vector<Edge> edges;
public:
    const std::vector<Vertex>& getVertices() const {return this->vertices; }
    const std::vector<Edge>& getEdges() const {return this->edges; }
    size_t makeVertex(const std::string& name);
    size_t makeEdge(size_t inId, size_t outId);
    Vertex& getVertex(size_t id);
    Edge& getEdge(size_t id);
    const Vertex& getVertex(size_t id) const;
    const Edge& getEdge(size_t id) const;
};

/**
 * Instances of class Vertex represent vertices of the Graph data structure.
 */
class Vertex
{
private:
    std::string name;
    size_t id;
    /** Vector of Edge's IDs pointing to this Vertex */
    std::vector<size_t> inEdgeIds;
    /** Vector of Edge's IDs pointing from this Vertex */
    std::vector<size_t> outEdgeIds;
    /**
     * Adds an Edge pointing towards this Vertex
     * @param id ID of the Edge pointing towards this Vertex
     */
    void addInEdgeId(size_t edgeId);

    /**
    * Adds an Edge which points out of this Vertex
    * @param id ID of an Edge which points out of this Vertex
    */
    void addOutEdgeId(size_t edgeId);

public:
    Vertex(std::string vertexName,
           size_t vertexId);
    const std::string& getName() const { return this->name; }
    size_t getId()   const { return this->id; }
    const std::vector<size_t>& getInEdgeIds()  const { return this->inEdgeIds; }
    const std::vector<size_t>& getOutEdgeIds() const { return this->outEdgeIds; }

    friend class Graph;
};

I need to complete the size_t makeVertex(const std::string& name) function.

/**
 * Factory function to make a new Vertex
 * @returns Id of the new Vertex
 */
size_t Graph::makeVertex(const std::string& name)
{
    (this->vertices).emplace_back(name); //this is what i have added
    return -1; // <- this return statement should change to give back the id of the new created vertex
}

My question is how can I get the vertex id of the vertex, that I just created?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source