2 Replies - 379 Views - Last Post: 09 August 2010 - 07:26 PM Rate Topic: -----

#1 ktalebian  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 08-April 08

Define an operator

Posted 09 August 2010 - 06:49 PM

Hi all,

Im using the OpenCV library with C++. I use the CvPoint type quite frequently, and I do a lot of operation on it. Its starting to get annoying to always have to compare the .x and the .y seperately.

How do I define an operator for =, +, -, / and * for the CvPoint type? More formally, how do I define an operator for an existing class?

Thanks
Kousha
Is This A Good Question/Topic? 0
  • +

Replies To: Define an operator

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Define an operator

Posted 09 August 2010 - 06:55 PM

There isn't really an easy way to do it. You would need to look at the source code and overload the operators. You can't just write a little extension.

But if you want to look at the source then it would probably be fairly easy to overload the operators.

Tutorial on operator overloading by KYA:
http://www.dreaminco...ding-operators/

This post has been edited by eker676: 09 August 2010 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,559
  • Joined: 09-June 09

Re: Define an operator

Posted 09 August 2010 - 07:26 PM

You dont define your own operators, you overload the ones that already exist :), Heres a quick example

-- Treat the class "Point" as your CVPoint because I dont have the OpenCV installed so I had to make a moc class


#include <iostream>

struct Point
{
	int x, y;
	Point(int _x, int _y)
		:x(_x), y(_y)
	{}
};

bool operator==(Point &pt1, Point &pt2)
{
	return (pt1.x == pt2.x) && (pt1.y == pt2.y);
}

int main()
{
	Point pt1(2,3);
	Point pt2(2,3);

	if(pt1 == pt2)
		std::cout<<"Points Are The Same"<<std::endl;
	else
		std::cout<<"Points Are Different"<<std::endl;

	

	std::cin.ignore();
	std::cin.get();
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1