There is the class A containing two overloaded methods getItems();
typedef std::vector <int> TItems;
template <typename T>
class A
{
private:
T a;
TItems items;
public:
A(){}
A ( const T a_, const TItems & items_) : a(a_) , items (items_) {}
bool operator () ( const A <T> &aa ) {return a < aa.a;}
TItems const & getItems() const {return items}
TItems & getItems() {return items}
};
and the set of A objects
template <typename T>
struct TSet {typedef std::set <A <T> > Type;};
I would like to return const reference / reference to TItems, but only the second method works
int main ()
{
TSet <double> ::Type t;
TSet <double> ::Type::iterator it = t.begin();
t.insert (A <double>( 5, TItems(10,10)));
const TItems *items = &(it->getItems()); //OK
TItems *items = &(it->getItems()); //Error
}
Error 1 error C2440: 'initializing' : cannot convert from 'const TItems *' to 'TItems *
Is it the reason that non-constant references enables to modify A objects causing a potential rearrangement od the set? But items of the set are not arranged by A.items but by a.
Is there a way how modify A.items using a non-constant reference?
Exactly.
std::set‘s elements (and BTWstd::map‘s keys) are immutable, the structure will only give you const-qualified elements. So you have the optionstd::mapand put youraas key anditemsas dataitems, you canconst_cast(or declareitemsmutableif that suits you).