Y
yccheok
I have an immutable object, where I do not provide implementation on
=operator. However, I am facing a problem when trying to use it with
stl map. stl map requires the object to have =operator being
overloading.
The following is the source of my immutable object:
Any suggestion on how to insert immutable object into the stl map is
very much appreciated.
Thank you very much!
cheok
#include "Point.h"
const Point Point::NULL_POINT = Point(-1, -1);
Point:oint() : x(-1), y(-1)
{
}
Point:oint(int _x, int _y) : x(_x), y(_y)
{
}
Point:oint(const Point &p) : x(p.x), y(p.y)
{
}
bool Point:perator< (const Point &p) const
{
// We will first convert this 2D Point to 1D point
// by using
// value = y * width + x where x will always < width.
//
if(this->y < p.y) {
return true;
}
else if(this->y > p.y) {
return false;
}
else {
return this->x < p.x;
}
}
bool Point:perator== (const Point &p) const
{
return ((this->x == p.x) && (this->y == p.y));
}
bool Point:perator!= (const Point &p) const
{
return ((this->x != p.x) || (this->y != p.y));
}
const int Point::getX() const
{
return this->x;
}
const int Point::getY() const
{
return this->y;
}
-----------------------------------
/*
* This is the immutable data structure used to represent location in
2D space.
*
* $Id$
*/
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point();
Point(int _x, int _y);
Point(const Point &p);
// Operator overloading. We need to perform < operator
// overloading so that this class can be stored in the
// STL collection classes (map).
//
bool operator< (const Point &p) const;
bool operator== (const Point &p) const;
bool operator!= (const Point &p) const;
const int getX() const;
const int getY() const;
// For invalid Point checking.
//
const static Point NULL_POINT;
private:
const int x;
const int y;
};
#endif
-----------------------------------
#include <map>
#include "Point.h"
using namespace std;
int main()
{
map<Point, Point> poitMap;
poitMap[Point(100, 100)] = Point(100, 100);
getchar();
return 0;
}
=operator. However, I am facing a problem when trying to use it with
stl map. stl map requires the object to have =operator being
overloading.
The following is the source of my immutable object:
Any suggestion on how to insert immutable object into the stl map is
very much appreciated.
Thank you very much!
cheok
#include "Point.h"
const Point Point::NULL_POINT = Point(-1, -1);
Point:oint() : x(-1), y(-1)
{
}
Point:oint(int _x, int _y) : x(_x), y(_y)
{
}
Point:oint(const Point &p) : x(p.x), y(p.y)
{
}
bool Point:perator< (const Point &p) const
{
// We will first convert this 2D Point to 1D point
// by using
// value = y * width + x where x will always < width.
//
if(this->y < p.y) {
return true;
}
else if(this->y > p.y) {
return false;
}
else {
return this->x < p.x;
}
}
bool Point:perator== (const Point &p) const
{
return ((this->x == p.x) && (this->y == p.y));
}
bool Point:perator!= (const Point &p) const
{
return ((this->x != p.x) || (this->y != p.y));
}
const int Point::getX() const
{
return this->x;
}
const int Point::getY() const
{
return this->y;
}
-----------------------------------
/*
* This is the immutable data structure used to represent location in
2D space.
*
* $Id$
*/
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point();
Point(int _x, int _y);
Point(const Point &p);
// Operator overloading. We need to perform < operator
// overloading so that this class can be stored in the
// STL collection classes (map).
//
bool operator< (const Point &p) const;
bool operator== (const Point &p) const;
bool operator!= (const Point &p) const;
const int getX() const;
const int getY() const;
// For invalid Point checking.
//
const static Point NULL_POINT;
private:
const int x;
const int y;
};
#endif
-----------------------------------
#include <map>
#include "Point.h"
using namespace std;
int main()
{
map<Point, Point> poitMap;
poitMap[Point(100, 100)] = Point(100, 100);
getchar();
return 0;
}