Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:09:44+00:00 2026-05-28T14:09:44+00:00

There is the class A containing two overloaded methods getItems(); typedef std::vector <int> TItems;

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:09:45+00:00Added an answer on May 28, 2026 at 2:09 pm

    Is it the reason that non-constant references enables to modify A objects causing a potential rearrangement od the set?

    Exactly. std::set‘s elements (and BTW std::map‘s keys) are immutable, the structure will only give you const-qualified elements. So you have the option

    • change your structure to std::map and put your a as key and items as data
    • if you are absolutely sure you won’t break the ordering by manipulation with items, you can const_cast (or declare items mutable if that suits you).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There two unrelated structures A and B template <typename T> struct A {}; template
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()
I found a Client.jar file on a customer's computer, containing two .class files which
I have a class containing a SortedList<string, Data> as private field, where Data is
Ok I have two modules, each containing a class, the problem is their classes
There are two assemblies: 1) Assembly containing serializer. This is a place from where
If there are two JAR files in the classpath, both containing a resource named
I have two QuerySets both containing the instances of the same model class. class
I have a templated class that is dependent on two template parameters to calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.