How to insert immutable object into the stl map

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::point() : x(-1), y(-1)
{
}

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

Point::point(const Point &p) : x(p.x), y(p.y)
{
}

bool Point::eek: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::eek:perator== (const Point &p) const
{
return ((this->x == p.x) && (this->y == p.y));
}

bool Point::eek: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;
}
 
Y

yuvalif

If you don't want anyone to assign a value into Point - except the STL
map, you should make the data members non-const, add a private
assignment operator and create class that inherits from STL map and is
a friend of Point.

Another thing, if you return by value its is already const, you don't
need to declare about it.

I hope it helped,

Yuval.
 
J

John Harrison

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.

[snip]


Like this

poitMap.insert(map<Point, Point>::value_type(Point(100, 100), Point(100,
100));

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top