gpt4 book ai didi

c++ - 将公共(public)属性设置为私有(private)

转载 作者:行者123 更新时间:2023-11-30 05:45:17 25 4
gpt4 key购买 nike

class vertex{

// ...

};

我正在尝试将此公共(public)属性设置为私有(private)。

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;
}

最佳答案

edges 应该 是私有(private)成员,可通过公共(public) 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 使用。那么,宣布这两个类(class)之间的友元就足够了吗?

以这种方式更改 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 类的所有私有(private)和 protected 成员,因为它已被声明为其友元。在简单的情况下,这是最快且侵入性较小的解决方案(但仅就需要编写的额外代码而言)。

关于c++ - 将公共(public)属性设置为私有(private),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29451593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com