How to cast the the property of type class

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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class Graph
{
public: 
vector <Edge *> graph;

void addAjEdges(orgNode,DstNode)
{
Edge *edge = new Edge(.....)
graph.push_back(edge);
}

void PrintGraph()
{
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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Graph
{
public: 
vector <Edge *> graph;

void addAjEdges(orgNode,DstNode)
{
Edge *edge = new Edge(.....)
graph.push_back(edge);
}
};

class Edge
{
public:
int orgNode;
int destNode;
....

//constructor
Edge(OrinatNode,....)
{
 orgNode = OrinatNode;
....
}
};
So, I used static_cast<Edge>(graphs[u]), but its giving the error.
1
2
3
4
5
6
7
8
9

void PrintGraph()
{
for (vector<int >::size_type u = 0; u < graphs.size(); u++) 
	{
	cout << " The Originating Edges is " << static_cast<Edge>(graphs[u]).orgNode << " ";
	}
}


I am not sure what seems to be the problem here.
Last edited on
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.


Edit:

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.
Last edited on
Thanks. Got it.
1
2
3
4
Edge edge = *graphs[u];

Then I was able to use the properties of the edge object.


Thank you for your suggestions!! Appreciated.
Any insight on smart pointer. I have heard about it but have not actually used it so far. If you don't mind could you please elaborate how it differs from regular pointer and how do i use them.
http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/smart_ptr.htm
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
By instance:
_ 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.
Thanks TheIdeaMan!
I did not had an intention to leave the properties on Edge classes public. I had made those private and used the method to return the properties value.

Next I will try to use List or map instead of vector. I am hoping it is straightforward to implement.

Thanks Ne555! - I will see if I can use auto_ptr<Edge> .

What do you mean by -
However, I think that you choose the worst option.
. Just curious. As you can tell, I am fairly new to c++ programming. So I am trying to learn the better ways.

Again I do appreciate all your help/feedback.
I mean that I don't think that you need to use pointers.

You choose `auto' so don't intend to share, and really doubt that you have polymorphic edges.
Thanks ne555!
Topic archived. No new replies allowed.