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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:54:21+00:00 2026-06-07T15:54:21+00:00

I am a Qt beginner and need to write some data classes. Would it

  • 0

I am a Qt beginner and need to write some data classes. Would it be a good approach to write all these classes as in the QSharedDataPointer example (here), or is this too much overhead (besides the fact it is more work)?

My classes in nature a very much like the below Employee class. I’ll have to deal with several hundreds of instances, not ten-thousands or millions.

A motivation for me to use QSharedData / QShareDataPointer would be the simplification of manual memory management and having a common code style. But I am still not sure whether I oversee some caveats.

From the example:

class EmployeeData : public QSharedData
 {
 public:
     EmployeeData();
     EmployeeData(const EmployeeData &other);
     ~EmployeeData();

     int id;
     QString *name;
 };

 class Employee
 {
 public:
     Employee();
     Employee(int id, const QString &name);

     void setId(int id) { d->id = id; }
     void setName(const QString &name);

     int id() const { return d->id; }
     QString name() const;

 private:
     QSharedDataPointer<EmployeeData> d;
 };
  • 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-07T15:54:24+00:00Added an answer on June 7, 2026 at 3:54 pm

    The advantages of doing the QSharedDataPointer/pImpl idiom would be:

    1) You can avoid some data copying in some cases (e.g. if you have two or more identical Employee objects, they can share the same EmployeeData back-end, rather than each having their own separate copy of that same data)

    2) After you add extra data fields to the EmployeeData class, only the source code for the Employee class would need to be recompiled, because only the Employee class (presumably) ever accesses the EmployeeData class directly. All your other code would (presumably) access the Employee class only, and therefore would not need to be recompiled because the Employee class’s size and layout would not have changed.

    Both of those reasons can be compelling in the right circumstances — Qt itself, for example, benefits from pImpl and implicit sharing in many places because Qt needs to handle large quantities of data extremely efficiently, and more importantly because Qt’s relationship with third-party developers requires it to guarantee that new Qt shared-library versions will remain backwards-compatible with existing third-party application executable code that was compiled against older Qt versions.

    For your personal program, however, it’s unlikely that you’ll see much benefit. If your data objects are as small/simple as the one shown in the EmployeeData example class (or even if they are 10 or 100 times bigger than that), the overhead of just making copies of the data will likely be minimal… and since you can presumably recompile your own codebase whenever you need to, the backwards-compatibility benefit doesn’t really matter for you.

    So my advice would be to keep it simple and just do things the standard C++ way, making regular old default-copy-constructor style copies of your member objects when necessary (and passing your objects to methods via const-reference when possible, to minimize the number of times they get copied). If you ever notice a measurable performance problem, at that point you could go back and rewrite some of your classes using pImpl/QSharedDataPointer to see if it gives you a measurable speedup (although you’ll likely find that it won’t).

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

Sidebar

Related Questions

I'm beginner and write some HTML code but what I need is when I
I am beginner with Java, and I would like to write some code like
JS beginner here. I need help with a script to place different content in
i'm a beginner to php. i need to use php function which process some
I am beginner in C++, I need to know which data structure to store
I am beginner in python, and I need to use some thirdparty function which
Instinctively, I would say that this is impossible, as NHibernate needs to know some
I am a beginner SQL user (not formally trained; OJT only) and need some
everyone, I am a beginner of developing android app, currently I need to write
I am a beginner with SQL and especially with TSQL. I need to write

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.