473,836 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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!

#include "Point.h"

const Point Point::NULL_POI NT = Point(-1, -1);

Point::Point() : x(-1), y(-1)

Point::Point(in t _x, int _y) : x(_x), y(_y)

Point::Point(co nst 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 {
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;

const int x;
const int y;



#include <map>
#include "Point.h"

using namespace std;

int main()
map<Point, Point> poitMap;

poitMap[Point(100, 100)] = Point(100, 100);

return 0;

Nov 22 '05 #1
2 2861
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,


Nov 22 '05 #2
yc*****@gmail.c om 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

Like this

poitMap.insert( map<Point, Point>::value_t ype(Point(100, 100), Point(100,

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 it contais cannot be changed, only reference will change to point to a different string. So results will be different in the following code depending is a, b are defined as String or StringBuffer: a = new String("test") (or new...
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 it is not optimal, for example for a large string it could be much faster to have it changed in place, not generating a new one for
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
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# and the CLR?" Anders' reply included this comment: "The concept of an immutable object is very useful, but it's just up to the author to say that it's immutable."
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.
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. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name* binds
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 immutable instances? -- \ "Love is the triumph of imagination over intelligence." -- |
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; }
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?" Ok. It's a broad, and maybe a very silly question to ask, but still. I mean, what good has it brought to us? What advantages immutable strings have against mutable ones?
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.