471,613 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,613 software developers and data experts.

How to insert immutable object into the stl map

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

bool Point::operator!= (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;
}

Nov 22 '05 #1
2 2757
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.

Nov 22 '05 #2
yc*****@gmail.com wrote:
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
Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Zalek Bloom | last post: by
7 posts views Thread by Torsten Mohr | last post: by
6 posts views Thread by Mapisto | last post: by
48 posts views Thread by Andrew Quine | last post: by
4 posts views Thread by Johnny Cash | last post: by
22 posts views Thread by Ben Finney | last post: by
90 posts views Thread by Ben Finney | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.