473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2833
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zalek Bloom | last post by:
I am learning about difference between String and StringBuffer classes. In a book it sayes that a String class is immutable, that means that one an instance of String class is created, the string...
7
by: Torsten Mohr | last post by:
Hi, reading the documentation (and also from a hint from this NG) i know now that there are some types that are not mutable. But why is it this way? From an overhead point of view i think...
6
by: Mapisto | last post by:
Hi, I've noticed that if I initialize list of integers in the next manner: >>> my_list = * 30 It works just fine, even if I'll try to assign one element: >>> id( my_list ) 10900116
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
4
by: Johnny Cash | last post by:
Can anyone tell me how to make a Hashtable immutable? I see that Hashtable has the "IsReadOnly" property, but I can't see how I am supposed to actually make it immutable. Thanks! BCOT.
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
3
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.