如何通过引用在参数内传递对象? [英] How do I pass an object by reference inside parameters?
本文介绍了如何通过引用在参数内传递对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了获得...
void MinPriority::createArray(string targetVertex, Graph & graph)
{
vector <list <Graph::Edge> >& adjList = graph.get_adjList();
}
工作我需要传递 Graph&图
通过另一个函数引用:
to work I need to pass in Graph &graph
by reference from another function:
void Graph::MST_PRIM()
{
MinPriority priority;
for(unsigned int i = 0; i != adjList.size(); i++)
{
priority.createArray(adjList[i].front().m_vertex, /*...*/);
}
}
/*...*/以获得createArray工作?
下面是类Graph的一个粗略的例子:
what would i put into /*...*/
to get createArray to work?
Here is a rough example of what class Graph looks like:
class Graph
{
private:
vector< list<Edge> > adjList;
public:
Graph();
~Graph();
class Edge
{
public:
Edge(string vertex, int weight)
{
m_vertex = vertex;
m_weight = weight;
}
~Edge(){}
string m_vertex;
int m_weight;
};
vector < list < Edge > > get_adjList(){return adjList;}
//Other functions....
};
如果您需要更多信息,请通知我。基本上我想访问adjList通过引用和使用它在createArray中使一个向量。 (在一个单独的类当然)
Please notify me if you need any more information. Basically I want to access adjList by reference and use it within createArray to make a vector out of it. (in a separate class of course)
推荐答案
似乎所有你需要的是传递 * this
:
It seems all you need is to pass in *this
:
priority.createArray(adjList[i].front().m_vertex, *this);
这篇关于如何通过引用在参数内传递对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文