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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:28:30+00:00 2026-05-14T04:28:30+00:00

I have two examples I have a question about. Let me explain via some

  • 0

I have two examples I have a question about. Let me explain via some code:

Question 1:

QStringList qsl(); // Create a list and store something in it
qsl << "foo";
QString test = "this is a test"; 
qsl = test.split(" ", QString::SkipEmptyParts); // Memory Leak?

What happens when I re-assign the qsl variable what happens to “foo” and the original data allocated on the first line?

Question 2:

class Foo
{
     QStringList mylist;
     void MyFunc(QStringList& mylist)
     {
           this->m_mylist = mylist;

     }

     void AddString(QString str)
     {
         mylist << str;
     }



}

int main()
{
    Foo f;
    QStringList *qsl = new QStringList();
    f.MyFunc(*qsl);
    delete qsl;
    f.AddString("this is a test"); // Segfault?
}

Here I’m passing a list by reference to a class which is then stored in said class. I then delete the original object.

It basically all comes down to what happens when you assign a QObject to a QObject. I assume a copy of the object is made, even if the object was passed in via reference (not via pointer of course, that would just be a pointer copy).

I also assume that something like QStringList performs a deepcopy…is this correct?

  • 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-14T04:28:30+00:00Added an answer on May 14, 2026 at 4:28 am

    Assigning to a QStringList variable works the same as assigning to any other variable in C++. For objects, the assignment operator of the object on the left is called to copy the content of the object on the right into the object on the left. Usually this does just a memberwise assignment:

    struct A {
      int x;
      QString y;
    
      A& operator=(const A &other) {
        // do the assignment:
        x = other.x;
        y = other.y;
    
        return *this;
      }
    };
    

    The object on the left of the assignment “adapts itself” to contain the same things as the object on the right. There is no new object allocated, just the existing one is modified.

    If the class is more complicated and for example contains pointers to dynamically allocated data (like it is probably is the case for QStringList), the assignment operator might be more complicated to implement. But this is an implementation detail of the QStringList class and you should not have to worry about that. The QStringList object on the left of the assignment will be modified to be equal to the object on the right.

    In Question 2 you assign an object to a member variable, which causes the object in the member variable to be modified so that it contains the same things as the object that is assigned to it. That this other object later is deleted doesn’t matter to the member variable.

    Semantically this is the same as when assigning simple integers:

    int i, j;
    i = j;
    

    The memory where i is stored is modified, so that it contains the same value as j. What happens to j later on doesn’t matter to the value of i.

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

Sidebar

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.