将公共属性设置为私有

时间:2015-04-04 21:29:43

标签: c++ methods attributes private dijkstra

class vertex{

    // ...

};

我尝试将此公共属性设置为私有。

vector<edge> edges;

但是这里

void addedge(int from, int to, int length=-1) {
    vertices[from].edges.push_back(edge(to, length));
}

在这里

edge &e = vertices[u].edges[i];

我无法理解如何使用像其他情况一样的方法来访问边缘。

完整代码:

#define MAX_VER 1000
#define INFINITE 9999
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
class edge{
    private:
        int to;
        int length;
    public:
        edge(int to, int length) : to(to), length(length) {}
        int getTo(){return to;};
        void setTo(int t){to=t;};
        int getL(){return length;};
        void setL(int l){length=l;};
};
class vertex{
    private:
        //vector<edge> edges;
        int dis;
        int prev;
    public:
        vector<edge> edges;

        vector<edge> get_edges(){return edges;}

        int getDis(){return dis;};
        void setDis(int d){dis=d;};
        int getPrev(){return prev;};
        void setPrev(int p){prev=p;};
};
class graph{
    private:
        vertex vertices[MAX_VER];
    public:
        void reset() {
            for (int i=0; i < MAX_VER; i++) {
                vertices[i].get_edges().clear();
                vertices[i].setDis(INFINITE);
                vertices[i].setPrev(-1);
            }
        }

        void addedge(int from, int to, int length=-1) {
            vertices[from].edges.push_back(edge(to, length));
        }
        typedef pair<int, int> pp;
        void dijkstra(int source) {
            priority_queue<pp, vector<pp>, greater<pp> > q;
            vertices[source].setDis(0);
            q.push(make_pair(0, source));
            while (!q.empty()) {
                int u = q.top().second;
                int dis = q.top().first;
                q.pop();
                if (dis > vertices[u].getDis())
                    continue;
                for (int i = 0; i < vertices[u].get_edges().size(); i++) {
                    edge &e = vertices[u].edges[i];
                    if (dis + e.getL() < vertices[e.getTo()].getDis()) {
                        vertices[e.getTo()].setDis(dis + e.getL());
                        vertices[e.getTo()].setPrev(u);
                        q.push(make_pair(vertices[e.getTo()].getDis(), e.getTo()));
                    }
                }
            }
            cout << "Distance from vertex 2 to 4 is: " << vertices[4].getDis() << endl;
        }
};
int main() {
    graph g;
    g.reset();
    g.addedge(0, 1, 5);
    g.addedge(0, 2, 9);
    g.addedge(0, 3, 4);
    g.addedge(0, 4, 6);
    g.addedge(1, 2, 2);
    g.addedge(1, 3, 5);
    g.addedge(1, 4, 7);
    g.addedge(2, 3, 1);
    g.addedge(2, 4, 8);
    g.addedge(3, 4, 3);
    g.dijkstra(2);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

edges 成为私人会员,可通过公共get功能访问。你已经定义了这样的东西(get_edges()),但它没有正确定义:

class vertex
{
    //...

public:
    vector<edge> edges;

    vector<edge> get_edges(){return edges;}

    //...
};

您通过值返回成员,这意味着,每次调用此函数时,都会创建edges新副本并返回该副本!

您应该通过引用返回它并为constnon-const对象提供版本:

class vertex
{
    //...

public:
    vector<edge> edges;

    vector<edge>& get_edges() { return edges; }
    const vector<edge>& get_edges() const { return edges; }

    //...
};

此外,edges仅由班级graph使用。那么这可能足以宣告这两个类之间的友谊吗?

以这种方式更改vertex课程:

class vertex
{
    friend class graph; //declare friend

private:
    int dis;
    int prev;
    vector<edge> edges; //make 'edges' private

public:
   vector<edge>& get_edges() { return edges; }
   const vector<edge>& get_edges() const { return edges; }
};

从现在开始,graph类将可以访问vertex类的所有私有和受保护成员,因为它已被声明为其朋友。在简单的情况下,这是最快且侵扰性较小的解决方案(但仅在需要编写的附加代码方面)。