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

  • Home
  • SEARCH
  • 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 3455150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:34:44+00:00 2026-05-18T09:34:44+00:00

Please conform if I am correct and tell me whether there is a better

  • 0

Please conform if I am correct and tell me whether there is a better solution:

I understand that objects with constant members like int const width; can not be handled by the synthetic assignment operator that is implicitly created by the compiler. But QList (and I suppose std::list, too) needs a working assignment operator. So when I want to use objects with constant members and QList I have three possibilities:

  1. Don’t use constant members. (Not a solution)
  2. Implement my own assignment operator.
  3. Use some other container that does not need assignment
    operators

Is that correct? Are there other elegant solutions?

Also I wonder whether I can:

  • (4) Force the compiler to create a assignment operator that deals with constant members! (I don’t understand why this is such a big problem. Why is the operator not intelligent enough to use initialization lists internally? Or am I missing something?)
  • (5) Tell QList that I will never use assignment operations in the list.

EDIT: I never assign objects of this class myself. They are only created by the copy constructor or by an overloaded constructor. So the assignment operator is only required by the container not by myself.

EDIT2: This is the assignment operator I created. I am not sure if its correct though. Cell has a two parameter constructor. These parameters set the two constant members with initialization lists. But the object also contains other variable (non const) members.

Cell& Cell::operator=(Cell const& other)
{
 if (this != &other) {
  Cell* newCell = new Cell(other.column(), other.row());
  return *newCell;
 }
 return *this;
}

EDIT3: I found this thread with almost the same question: C++: STL troubles with const class members All answers combined together answered my questions.

  • 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-18T09:34:45+00:00Added an answer on May 18, 2026 at 9:34 am

    I’ll try to bundle the answers in short:

    The main problem is that QList requires the assignment operator to be present because they internally use assignment. Thus they mix implementation with interface. So although YOU don’t need the assignment operator QList won’t work without it. source

    @ 3. There is std::List but it doesn’t offer constant time access to elements, while QList does.

    @ 2. It is possible by creating a new object with the copy constructor and the desired properties and returning it*. Although you circumvent the const property it is still better than using no const at all because you would allow the container to cheat here but still prevent users to do this themselves which was the original intention of making this member constant.

    But take into account that creating an overloaded assignment operator adds to the complexity of the code and might introduce more errors than the const-ing of the members would solve in the first place.

    @ 1. In the end this seems to be the easiest solution. As long as it’s private you just have to pay attention that the object doesn’t change it itself.

    @ 4. No way to force him. He wouldn’t know how because the variable is constant and at some point he would have to do this->row = other.row with int const row; previously defined. And const means constant even in this case. one source

    @ 5 QList has no options of this kind.

    Additional solutions:

    • Use pointer to objects instead of pure objects

    *Not sure about this at the moment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF RIA Domain Service that contains a method I'd like to
This question may seem like a novice, and perhaps 'stupid' question but please bear
Can someone please confirm to me that when my application is written in .NET
Below is the drupal custom modules, can u please confirm it, is it correct
I'm a CSS newbie. Please help confirm if my understanding is correct. I have
can someone please confirm/correct me, with my understanding of using POCO's with the Entity
Please see the code below. I am trying to draw a circle around a
Please try egrep ^[a-z]{3}$ /usr/share/dict/words egrep ^[[:lower:]]{3}$ /usr/share/dict/words The first one returns both uppercase
Please have a look at http://500px.com/photo/3977429?from=popular . Down in the right corner, theres a
Please take a look at this image: I apologise for my useless paint ability

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.