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 9131769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:11:53+00:00 2026-06-17T08:11:53+00:00

I am having a bit of a problem with Qt’s foreach function. I have

  • 0

I am having a bit of a problem with Qt’s foreach function. I have a class Phrase, which is a subclass of QList. In ~Phrase I delete all of the GlossItem pointers.

In iterating through the GlossItem pointers in Phrase, I would like to use Qt’s foreach:

    // phrase is a pointer to a Phrase object, 
    // which is a subclassed QList<GlossItem*>
    foreach( GlossItem *glossItem , *phrase )
    {
        // use glossItem
    }

For some reason foreach is performing a deep copy on Phrase (I know this because it required me to implement the copy constructor). But if there is a copy of Phrase — and if I don’t want to create a deep copy of each GlossItem — that means that those pointers will be deleted twice. (Or, deleted once, and then crash.) So I have to use this, which works but is less pretty.

    for(int i=0; i<phrase->count(); i++ )
    {
        GlossItem *glossItem = phrase->at(i);
        // use glossItem
    }

Is there a way around this or do I just need to live with it?

  • 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-06-17T08:11:54+00:00Added an answer on June 17, 2026 at 8:11 am

    From the docs

    Qt automatically takes a copy of the container when it enters a foreach loop.

    and

    Since foreach creates a copy of the container, using a non-const reference for the variable does not allow you to modify the original container.

    So I think for your specific use case foreach isn’t suitable otherwise you end up with a fresh copy of the Phrase with additional pointers, rather than returning the actual original Phrase that you want.

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

Sidebar

Related Questions

I'm having a little bit of problem with my derived class. Basically I have
I'm having a bit of a problem. We have our own CMS which is
I'm having a bit of a problem with a Prolog exercise. Assume i have
I'm having a bit weird problem with WinForm which seems to refuse to close
Having a bit of a problem with the AJAX Accordion Control... I have two
i'm having a bit of a problem. Here's the situation. I have an amount
I am having a little bit problem on the MapView. First of all, no
I am having a bit of problem regarding Swing. I have a JFrame called
I'm having a bit of a problem I have created a post and it
I am having a bit of a problem here. I have two int values,

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.