|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1846
|
[C++] Reference a oggetto di una classe sembra non essere valorizzato
Sto implementando questa classe:
Codice:
typedef Graph<GraphNode, GraphEdge> GraphType;
template <class graph_type, class node_type, class edge_type>
class DijkstraSearch
{
public:
typedef node_type Node;
typedef edge_type Edge;
typedef graph_type Graph;
private:
//the graph the algorithm is runned on
const Graph& graph;
//stores the closest node to compute in the next step
int nextClosestNode;
//Search Path Tree built by the algorithm
std::vector<const Edge*> spt;
//cost to reach each node from the source (the index inside the vector is the index of the node)
std::vector<double> costToThisNode;
//vector containing parent edges leading to the node connected to the spt but not added to the spt yet
std::vector<const Edge*> searchFrontier;
//source and target for the search
int source;
int target;
void Search();
public:
DijkstraSearch( const Graph& p_Graph,
int p_Source,
int p_Target = -1)
:
graph(p_Graph),
spt(p_Graph.NumNodes()), //init with the number of nodes in the graph
searchFrontier(p_Graph.NumNodes()), //init with the number of nodes in the graph
costToThisNode(p_Graph.NumNodes()), //init with the number of nodes in the graph
source(p_Source),
target(p_Target)
{
//std::cout<<"p_Source: "<<p_Source<<std::endl;
//std::cout<<"p_Target: "<<p_Target<<std::endl;
//p_Graph.PrintGraph();
std::cout<<"searchfrontier size: "<<p_Graph.NumNodes()<<std::endl;
//std::cout<<"costToThisNode size: "<<costToThisNode.size()<<std::endl;
Search(); //run the search when instantiated
}
std::list<int> GetPathToTarget()const;
};
Il main è questo: Codice:
int _tmain(int argc, _TCHAR* argv[])
{
//istantiate a graph
GraphType *graph = new GraphType(true);
//graph->PopulateRandom(5, 8);
graph->CreateTestGraph();
//graph->PrintGraph();
//test dijkstra algorithm
DijkstraSearch<GraphType, GraphNode, GraphEdge> *dijkstraSearch = new DijkstraSearch<GraphType, GraphNode, GraphEdge>( graph, 0, 2);
//print the resulting SPT
dijkstraSearch->GetPathToTarget();
delete graph;
system("pause");
return 0;
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1846
|
Problema risolto.
La chiamata deve essere: Codice:
DijkstraSearch<GraphType, GraphNode, GraphEdge> *dijkstraSearch = new DijkstraSearch<GraphType, GraphNode, GraphEdge>( *graph, 0, 2); (Per punirmi sto per guardare un video di 48 minuti solo sui puntatori). |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Come l'altra volta...ma evitarli, questi puntatori, no??
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1846
|
Mi sono fatto il lavaggio del cervello. Spero di ricordarmene per le prossime volte
Mi resta da approfondire i reference e come funzionano i template dal punto di vista dei puntatori. |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1846
|
Ah è sempre riguardo la stessa cosa, non ho ancora digerito bene alcune cose e devo approfondire la teoria che c'è dietro.
Per esempio qui: Codice:
template <class node_type, class edge_type>
int Graph<node_type, edge_type>::AddNode(node_type node)
{
//trying to add a node that was already in the graph
if ( node.Index() < (int)nodes.size())
{
......
....
...
}
Codice:
GraphNode *node = new GraphNode(i); AddNode(*node); Poi la funzione lo usa senza * perchè ha il valore del nodo e non l'indirizzo di memoria (tant'è che come sintassi posso usare il punto '.' invece del '->' che risolve il puntatore come si farebbe con '(*node).' ). Dunque devo desumere che node lì è passato per copia? Se io modifico node in realtà sto modificando solo il node che è nello scope della funzione? Probabilmente sì (ma anche probabilmente no visto che non sono molto convinto di quello che ho detto) ma devo studiarmi un po' la teoria. Lo stesso per gli iterator che mi pare vengano sempre considerati come puntatori ma nelle guide che ho seguito faceva solo un breve accenno a questa cosa e vorrei almeno rileggerla. |
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Quote:
E' logico che sia la funzione a decidere se poter modificare un oggetto passato come parametro, perchè se così non fosse una stessa funzione potrebbe essere usata con effetti diversi scegliendo da fuori se passare un reference o una copia (a parte che non c'è la sintassi per farlo, parlo giusto in linea teorica). |
|
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
Ci sono ottimi motivi per usare sia gli uni che gli altri.
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Uno dei pochi motivi per usare i puntatori nudi è come reference a oggetti già esistenti, quando viene scomodo dover per forza inizializzare una reference (o se non puoi usarle, come in un vector<T&> che non puoi fare, devi usare vector<T*>). Altrimenti meglio usare una delle nuove classi, o oggetti veri e propri definendo dei move constructor.
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:56.




















