Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-12-27 11:59:47


----- Mensaje original -----
De: damny_at_[hidden]
Fecha: Martes, Diciembre 25, 2007 5:17 pm
Asunto: [Boost-users] [multi_index] passing 'cons t TYPE' as 'this'
argument discards qualifiers
Para: boost-users_at_[hidden]

> merry xmas everyone,

Merry Xmas :)

[...]
> class cClass {
> private:
> int att1;
> public:
> std::string nme; // name
> int idf; // identifier
> cClass();
> cClass(std::string nme, int idf);
> int get_attribute();
> };
>
[...]
> mMapByNme::iterator itrNme = locMsg.get<0>().find("name1");
> mMapByIdf::iterator itrIdf = locMsg.get<1>().find(456);
> printf( "att1 = %d\n", itrNme->get_attribute() );
[...]
> main.cpp: In function `int main()':
> main.cpp:33: error: passing 'const cClass' as 'this' argument of
> 'int cClass::get_attribute()' discards qualifiers
> Process terminated with status 1 (0 minutes, 20 seconds)
>
> any hints how to solve the problem?

The problem here is that elements of a multi_index_container
are immutable, and the iterators ony grant const access
to them. In this regard, the behavior is the same as that
of a regular std::set. To solve your particular problem you can
const-qualify the offending member function:

  int get_attribute() const;

> how to encapsulate this special multi_index into a class, that
> fits the above description of the needed 'map'.

You should probably consider whether Boost.Bimap could
be a better approach, but I see you're already discussing
this issue with Matías.

> are there possibilities to avoid the 'std::string' for the 'name'
> key?

I'm not getting this question. Could you please elaborate?

> where are the more easier examples for C++-templates/boost
> newbies like me :o) ?

HTH,

Joaquín M López Muñoz
Telefónica, Investiación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net