Vague Struct

May 23, 2012 at 2:25pm
hi...
I have piece of c++ code that I didn't get it.
It's just a struct.
Is there anyone could help me to understand this code.
here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
struct edge
{
	int p, q, cost;
	edge(int _p = 0, int _q = 0, int _cost = 0)
	{
		p = _p, q = _q, cost = _cost;
	}
	bool operator<(const edge &two)const
	{
		return cost < two.cost;
	}
};


Thank's alot
May 23, 2012 at 2:29pm
This is a struct. The kind of struct is an "edge".

It has three member variables. They are integers. They are named p, q and cost.

It has one member function. A constructor. To create an object of type edge, you will have to use that constructor. The constructor provides default values, so if you don't provide any variables when you construct one, some default values will be used.

It has one overloaded operator. The operator "<". This operator is used to compare two variables of type "edge". It is used to see if the "cost" member variable of one edge is less than that of another edge.

You are reminded that in C++ the difference between a struct and a class is the default level of member visibility. In C++, a struct has member functions. It has constructors. It is just like a class.
Last edited on May 23, 2012 at 2:31pm
May 23, 2012 at 2:40pm
so, ex. we make an object from this type, and then print it's variable on the screen...

1
2
edge e;
cout << e.p << " " << e.q << " " << e.cost << endl;


is that:


0 0 0


am I true?
May 23, 2012 at 3:22pm
Build and test it.
May 23, 2012 at 3:42pm
Yeah...I'm true
thanks but I have another problems with operator overloading...
1.The "<" operator needs 2 int as input to operate but here we have just one input...

const edge &two

2.what is the alone const word at the end of the line that operator overloaded? what act does it?

3.
return cost < two.cos;

does it say:

if (cost < two.cost) return ?;
May 23, 2012 at 3:52pm
Sounds like you simply need to read up on operator overloading.
May 23, 2012 at 4:00pm
yeah..sure
could you please answer my 3 questions and after that introduce a good article about operator overloading??
Topic archived. No new replies allowed.