473,769 Members | 5,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::map const troubles

I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

If I replace "map<Foo*,i nt>" with "map<const Foo*,int>" or if I change
"lookup(con st Foo& f)" with "lookup(Foo & f)" suddenly it works. Why?
I am befuddled.

How do I get this to work, without changing the signatures?
Jul 22 '05 #1
4 1462
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

lookup it supposed to return an int and I dont see it returning one.

Raj
Jul 22 '05 #2

"Grey Plastic" <gr*********@ho tmail.com> wrote in message
news:1d******** *************** ***@posting.goo gle.com...
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
I assume you mean something like this

int lookup(const Foo& f) const { return myMap.find(&f)->second; }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?
It is using the const version, but the const version takes a Foo* parameter
and you are giving it a const Foo* parameter.

If I replace "map<Foo*,i nt>" with "map<const Foo*,int>" or if I change
"lookup(con st Foo& f)" with "lookup(Foo & f)" suddenly it works. Why?
See above.
I am befuddled.

How do I get this to work, without changing the signatures?


int lookup(Foo f) const { return myMap.find(&f)->second; }

That works, at the cost of copying a Foo object.

john
Jul 22 '05 #3
Grey Plastic wrote in
news:1d******** *************** ***@posting.goo gle.com in comp.lang.c++:
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

If I replace "map<Foo*,i nt>" with "map<const Foo*,int>" or if I change
"lookup(con st Foo& f)" with "lookup(Foo & f)" suddenly it works. Why?
I am befuddled.

How do I get this to work, without changing the signatures?


The key type of the map is `Foo * const` your lookup() function passes
in a `Foo const *` this cast *requires* const_cast<>, as even though
you are adding a const qualifier, you are also removing one.

IOW:

Foo const * -> Foo const * const, is ok but you are doing
Foo const * -> Foo * -> Foo * const.

The good news is that since std::map doesn't dereference the
'key' you can safely use const_cast<>().
int lookup( Foo const &f )
{
myMap.find( const_cast< Foo * >( &f ) );
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
> >
How do I get this to work, without changing the signatures?


int lookup(Foo f) const { return myMap.find(&f)->second; }

That works, at the cost of copying a Foo object.


It's rubbish. It compiles but it won't work because the address of the
object passed to find will be different from the address of the object used
to call lookup. Apologies.

john
Jul 22 '05 #5

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

Similar topics

24
17328
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
1
1869
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
44
8810
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice, I chose to use std::map. However, the program runs at less than half the speed of an equivalent program that uses ordinary handcoded binary trees. The result is not changed very much if I replace std::string as the key with a simple string class...
1
1973
by: Bob | last post by:
Hi, I'm trying to use a map with a string key, and a pointer to objects contained in a vector. I've wrapped this in a class like so: // cMap template<class T> class cMap : public cList<T> { // private: protected: std::map<std::string, T*> tMapOf; // map container
2
3405
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
1
3553
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
3
3715
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
13
9678
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
2401
by: mergaite_lietuvaite | last post by:
Hi, I try to use map. My code is very easy, but there are some errors.... (by compiling). Plaese, could you say, what I do wrong.. struct less_array { bool operator()(const FArray p1, const FArray p2) const { return p1 < p2 ;
0
9423
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,...
0
10215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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,...
1
7410
isladogs
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...
0
6674
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();...
0
5307
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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.