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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:51:54+00:00 2026-06-18T22:51:54+00:00

I have the following setting: class A { public: A(); virtual ~A(); A(const A&other)

  • 0

I have the following setting:

class A {
  public:
  A();
  virtual ~A();
  A(const A&other) {*this = other;}
  A& operator=(const A&) {/*assigne reference members, create new of pointer members*/}

  /* some other stuff */
}

class B : public A {
  public:
  B();
  virtual ~B();
  B(const B&other) {*this = other;}
  B& operator=(const B&rhs) { A::operator=(rhs); /*assigne reference members, create new of pointer members*/}

  /* some other stuff */
}

class C : public A {
  public:
  C();
  virtual ~C();
  C(const C&other) {*this = other;}
  C& operator=(const C&) { A::operator=(rhs); /*assigne reference members, create new of pointer members*/}

  /* some other stuff */
}


class D {
  public:
  D();
  virtual ~D();
  D(const D&other) {*this = other;}
  D& operator=(const D&rhs) {
    /* iterate through map and create new instances of B or C */
    m_list = rhs.m_list;
  }

  QMap<QUuid, A*> GetMap() {return m_map;}
  QList<QUuid> GetList {return m_list;}

  private:
  QMap<QUuid, A*> m_map;
  QList<QUuid> m_list;

  /* some other stuff */
}

Now I put some B and C into the map get a reference from D which creates a deep copy of the QMap through D‘s copy constructor. If I try to get the size of the QMap it is working but the list is corrupted. With help of the debugger I figured out that the QList in D gets corrupted when the assignment operator of the QMap calls std::swap. It is completely unclear for me what happens there with my memory.

Has this something to do with the derivation and using the base class pointer? If I change the QMap to std::map my program also crashes but at another point with another problem.

Thanks for any advices and hints.

  • 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-18T22:51:55+00:00Added an answer on June 18, 2026 at 10:51 pm

    Now I put some B and C into the map get a reference from D which
    creates a deep copy of the QMap through D’s copy constructor.

    The map is copied but you then have two maps pointing to the same objects, the question is then who owns the objects.

    It would be better that you instead of storing raw pointers use shared_ptr to your instances

    QMap<QUuid, std::shared_ptr<A>> m_map;

    Also note that your method GetMap() returns a copy of the map, not the actual map so when you call that you have one more map that maps to the same objects.

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

Sidebar

Related Questions

I have the following: class Base { protected: std::string _name; public: virtual ~Base(){} const
I have the following: public class MyClass : SuperClass { [JsonProperty] public virtual string
I have following this blog in setting Xvfb in my ubuntu environment: http://corpocrat.com/2008/08/19/how-to-install-xvfb-x11-server-in-linux-server/ So
I have the following windows service file: Imports System.ServiceProcess Imports System.IO Public Class fswService
In out program, we have the following class hierarchy: public class PagePanelBase: UserControl public
I have the following class: namespace Controls { public class DropDownButton : Control {
Let's say I have the following request object: [DataContract] public class MyContract { [DataMember]
I have the following code: import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; public final class
I have the the following classes: class Base { public: Base() { x =
Basically I have a simple base class: public abstract class BasePoco { public virtual

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.