I have similar thread earlier but this issue is bit different than the earlier one. So creating a new thread.
So, from the code below, what I am trying to know is if its possible to cast the graph property inside Graph class, so that I can access Edges Property that are assigned to that graph.
So, in the Graph class I have a new method -PrintGraph()
vector <Edge *> graph;
Edge *edge = new Edge(.....)
for (vector< vector<int> >::size_type u = 0; u < graphs.size(); u++)
cout << " The Originating Edges is " << ((Edge)graphs[u]).orgNode << " ";
Here I am not sure how do I use - ((Edge)graphs[u]).orgNode
So, I used static_cast<Edge>(graphs[u]), but its giving the error.
¿and the error is?
I am not sure what seems to be the problem here.
The vector has pointers, you're treating them as objects. vector <Edge *> graph; should be vector <Edge> graph;
You want to use pointers when:
_ There is polymorphism in the elements
_ Need to share the elements with other containers. However, to handle the life-time:
____ Use smart pointers (shared pointers), or some kind of garbage collector.
____ Don't handle it, it's not your responsibility.
To access a member variable, you need to provide a public member function with that class.
Although you have made everything public - itself not a good idea.
When the Edge object is part of the Graph object, you need to first access the Graph object with one of it's functions, from there access the Edge object with one of it's functions.
Also, I am wary of using vectors of vectors - they can be rather inefficient when the get bigger. Perhaps you could have a <list> of vectors, or a list of Edge objects. Or consider other containers like <set> if you want them to be sorted for example. Or <map> if you want to have a lookup of the objects.
Is the edge itself going to grow in size? If the edge is going to have a constant number of member variables then it could just remain a class. Graph edges normally have a fixed number of attributes like distance & cost etc.
An smart pointer is simply a wrapper for pointers (raw pointers).
When a raw pointer goes out of scope nothing happens. So if it was pointing to allocated memory you may have a leak.
When you copy raw pointers, you copy the address they are pointing to, not the contents.
If that behaviour is not wanted, you use an smart pointer that does what you want to do
_ auto pointer. When out of scope it will delete what it points. It does not allow copies.
_ copy pointer. delete when out of scope. Copy the contents (clone pattern)
_ shared pointer. Copy the address. The last one to die will destroy the object.
To use them you simply say auto_ptr<Edge> instead of Edge*
However, I think that you choose the worst option.