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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:25:40+00:00 2026-06-08T11:25:40+00:00

I have a class like the following: class A { SuperHugeClass* s; public: A(){

  • 0

I have a class like the following:

class A {
  SuperHugeClass* s;
public:
  A(){ s = new SuperHugeClass(); }
};

Because SuperHugeClass takes a lot of memory, I’m fine with the shallow copying provided by the default constructor and assignment operator. However, I also don’t want to leak memory, so I need to delete s, but I have to be careful about it because otherwise I’ll delete it more than once.

One way of doing this is by refcounting s as follows:

class A {
  int* refcount;
  SuperHugeClass* s;

public:
  A(){
    refcount = new int(1);
    s = new SuperHugeClass();
  }

  A(const A& other) : refcount(other.refcount), s(other.s) {
    (*refcount)++;
  }

  ~A() {
     (*refcount)--;
     if (!(*refcount)) {
       delete refcount;
       delete s;
     }
   }

  friend void swap(const A& a, const A& aa) {
    std::swap(a.refcount, aa.refcount);
    std::swap(a.s, aa.s);
  }

  A& operator=(A other) {
    swap(*this, other);
    return (*this);
  }
}; 

This is the first time I’ve needed to do something like this, but it seems to me that this should be pretty standard and so there should be a ‘canonical’ solution. Are there any other ways of doing this? Thanks!

  • 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-08T11:25:42+00:00Added an answer on June 8, 2026 at 11:25 am

    Use std::shared_ptr

    class A {
      std::shared_ptr<SuperHugeClass> s;
    public:
      A()
      : s(new SuperHugeClass())
      {
      }
    };
    

    and thats it. Default generated copy constructor/assignment operator/destructor do just what you need.

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

Sidebar

Related Questions

I have a class like the following: class node { public: node* parent; std::list<node*>
I have a class that looks like the following: public class MyClass { private
We have a class a class that looks something like the following: public class
I have a model like the following one: public class TestModel{ public IList<Field> Fields
I have a class like following: public class Trainee { private static int numberOfTrainee
I have a class that look like the following: public class MyClass { ...
I have class like the following: class test { public string Name; public string
Suppose I have a class like the following: public class Stage { public int
I have an enum class like the following: public enum Letter { OMEGA_LETTER(Omega), GAMMA_LETTER(Gamma),
I have a class that looks like the following. This class compiles fine on

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.