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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:06:54+00:00 2026-05-30T02:06:54+00:00

Im creating a model in qt and came across this code: class StringListModel :

  • 0

Im creating a model in qt and came across this code:

class StringListModel : public QAbstractListModel
{
     Q_OBJECT

public:
    StringListModel(const QStringList &strings, QObject *parent = 0)
    : QAbstractListModel(parent), stringList(strings) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                     int role = Qt::DisplayRole) const;

private:
   QStringList stringList;
};

Now I wonder, does this work? If a pass a qstringlist into this function thats localeted of the stack, and it runs out of scope, wont this object loose its stringlist?

Read both:
C++ reference in constructor
and:
Constructors accepting string reference. Bad idea?

.. where some people say that it will be invalid, but some say that the string (in their examples) will be copied to the local variable. This is really confusing.

  • 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-30T02:06:55+00:00Added an answer on May 30, 2026 at 2:06 am

    If I understand what you’re asking, you’re concerned because the QStringList &strings is passed by reference?

    The initialization list calls the stringList(const &QStringList) copy-constructor. This copy constructor copies the state of the passed &QStringList object to a new object and assigns that object to stringList This way even if the original &strings goes out of scope or is otherwise destroyed it won’t matter since stringList points to a different object.

    In your second link there is no copy-constructor invoked… the member object is assigned to point at the same object that was passed to it. If the passed object is destroyed later in the program the member object would still be pointing to that destroyed object, making it invalid.

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

Sidebar

Related Questions

Creating a mini-database with access, i came across this problem: For the background, i
I am creating a model binder to use with asp.net mvc. This is what
My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,
I'm creating a model in Rails 3 as follows: class Foo < ActiveRecord::Base attr_accessible
I'm following Steve Sanderson's example from this ASP.NET MVC book on creating a model
I tried creating a model called class (as in a graduating class of students),
I'm looking for a free and simple tool for creating relational model diagrams. All
I am creating a Category model and using the awesome_nested_set plugin (a replacement for
I am creating the domain model in my system. When designing my model objects,
Sometimes the overhead of creating a new model type is annoying. In these cases,

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.