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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:21:00+00:00 2026-05-26T17:21:00+00:00

I’m having troubles choosing the best solution to my problems. Currently, I’m working on

  • 0

I’m having troubles choosing the best solution to my problems. Currently, I’m working on my first C++ project, I’m using Qt BTW, but because I’m a .NET developer, there are some things that bit me concerning C++ :).
As you all know, in a gargage collected world, such as Java or .NET, best practice is to use Dependency Injection for loose coupling classes, making it more testable and maintenable, but I have no idea what to do in C++. C++ doesn’t have a GC, so we should take care about every memory allocation and that causes too much questions.

For example, I have a class “Rule”, which has a member field pointing to “Application” class:

class Rule : public QObject
{
public:
    explicit Rule(QObject *parent = 0);

    unsigned int id();
    void setId(unsigned int id);

    Application* application();
    void setApplication(Application* application)
    {
        m_Application = application
        m_Application->setParent(this);
    }

};

In the constructor, I’m assigning NULL to application instance variable. In setApplication, I’m assigning this as the parent to application. Application instance will be deleted automatically when Rule (the parent) is destroyed, thanks to Qt. Is this a good approach? would it be better if I used smart pointer such as QSharedPointer instead of Application*? What’s your experience, what’s the drawbacks, what’s the best approach. I would gladly listen to your advices. Also there is another tricky part here. What if I gave that class to another developer in the team, or I published the library. The developer could easily write something like:

Application app;
app.setId(1);
Rule rule;
rule.setApplication(&app); //When rule will be destroyed, the program would crash because app is allocated on the stack.

or

Application *app = new Application();
app->setId(20);
Rule *rule = new Rule();
rule->setApplication(app);
Application *appToAnotherLocation = new Application();
rule->setApplication(appToAnotherLocation); // This wouldn't result in memory leak, because app is already child of rule, but if I didn't used Qt this would be a problem... probably :)

Now what about smart pointers? Is there some rules, when we should use them and when we shouldn’t? I have a IRepository interface, which always returns QSharedObject instead of pointers. Is this a good approach or am I overusing it?

class IRepository
{
public:
    virtual bool save(Application & application) = 0;
    virtual bool save(Rule & rule) = 0;
    virtual bool save(History & history) = 0;

    virtual bool remove(Application & application) = 0;
    virtual bool remove(Rule & rule) = 0;
    virtual bool remove(History & history) = 0;

    virtual QSharedPointer<Application> getApplication(unsigned int id) = 0;

    virtual QSharedPointer<Rule> getRule(unsigned int id) = 0;
    virtual QList< QSharedPointer<Rule> > getRules(unsigned int applicationId) = 0;

    virtual QSharedPointer<History> getHistory(unsigned int id) = 0;
    virtual QList< QSharedPointer<History> > getHistories(unsigned int applicationId) = 0;
    virtual QList< QSharedPointer<History> > getHistories(unsigned int applicationId, QDateTime dateFrom, QDateTime dateTo) = 0;
};

Thanks.
Your help is much appreciated.

  • 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-26T17:21:01+00:00Added an answer on May 26, 2026 at 5:21 pm

    Subjective opinion;

    First: like Chris said, smart pointer are a very good alternative for memory leaks. Try always to use them.

    Second: a good design approach is to delete pointers only in creators. I mean, one class creates and destroys their pointers. Don’t delete pointers in another classes, this will allows you to have more clear, readable and maintainable code.

    In your example, Rule ‘must’ not delete Application pointer. Think about your design: I don’t understand why a Rule is an Application parent. I suppose that an Application may work with a lot of rules.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.