Home > Enterprise >  C weird instantiation of struct
C weird instantiation of struct

Time:10-02

I have a struct in a header file as shown here

struct GraphNode {
        Id id {};
        std::string name {};
        long long int passengerCount {0};
        std::vector<std::shared_ptr<GraphEdge>> edges {};

        // Find the edge for a specific line route.
        std::vector<
            std::shared_ptr<GraphEdge>
        >::const_iterator FindEdgeForRoute(
            const std::shared_ptr<RouteInternal>& route
        ) const;
    }; 

I'm not sure about the "find the edge for a specific line route" part of the code. So is FindEdgeForRoute a vector of shared pointers to type GraphEdge, but instead of a normal vector it's an iterator? I'm confused.

It's also instantiated in this particular manner:

bool TransportNetwork::AddStation(
    const Station& station
)
{
    // Create a new station node and add it to the map.
    auto node {std::make_shared<GraphNode>(GraphNode {
        station.id,
        station.name,
        0, // We start with no passengers.
        {} // We start with no edges.
    })};
    return true;
} 

Is the above the same as if I do

bool TransportNetwork::AddStation(
    const Station& station
)
{
        GraphNode node;          
        node.id = station.id;
        node.name = station.name;
        node.passengerNumber = 0;
}

And I don't know how I would initialize the FindEdgeForRoute function. Can anyone help clarify this?

CodePudding user response:

is FindEdgeForRoute a vector of shared pointers to type GraphEdge, but instead of a normal vector it's an iterator?

What you see in the struct definition is that it declares a const member function called FindEdgeForRoute that takes one parameter (a const std::shared_ptr<RouteInternal>&) and returns a const_iterator into a std::vector<std::shared_ptr<GraphEdge>>.

I don't know how I would initialize the FindEdgeForRoute function.

You need to call it with a std::shared_ptr<RouteInternal>.

  •  Tags:  
  • c
  • Related