search

Home  >  Q&A  >  body text

c++ - 两个类如何能够相互使用?

比如这种情况

class Node {
public:
    int index;
    std::vector<Edge> outs;
};

class Edge {
public:
    double weight;
    Node vertex;
};
大家讲道理大家讲道理2806 days ago508

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 13:15:20

    Let me make a statement first.

    #include<vector>
    
    class Edge;
    
    class Node {
    public:
        int index;
        std::vector<Edge> outs;
    };
    
    class Edge {
    public:
        double weight;
        Node vertex;
    };
    

    Strictly speaking, Node should use Edge pointers or references, or explicitly support incomplete type containers. However, generally vectors are implemented based on pointers, so there is no problem.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:15:20

    Calling classes to each other feels like there is something wrong with this design

    reply
    0
  • Cancelreply