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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:46:18+00:00 2026-05-28T17:46:18+00:00

Imagine the following classes in java or C#: class A { B b; //some

  • 0

Imagine the following classes in java or C#:

class A
{
  B b;
  //some other stuff

  public A(B b) {this.b = b;}
}

class B
{
  A createA() {return new A(this); }
}

then we would use it e.g.

A complicatedCreateA()
{
  B = new B();
  return b.createA();
}

and the virtual machine / CLR would make sure we don’t leak memory.

How can I implement a similar pattern in C++ so that I don’t leak memory and reference cleaned resources?

EDIT:
To make it more clear I am specifically worried about what happens if I call createA() more than once, and when the different objects A will have different lifetimes, e.g.:

A anotherMethod()
{
  B = new B();
  A a = b.createA();
  //use a locally, or use with yet another object C, etc.
  return b.createA();
}

I have the basic understanding how smart pointers work in C++. However, even if I do something like:

boost::shared_ptr<B> b(new B());

then I don’t have access to this smart pointer from within B, so I can’t pass it to A. And how otherwise A can make sure that the corresponding object B gets deleted not too late and not too early?

  • 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-28T17:46:18+00:00Added an answer on May 28, 2026 at 5:46 pm

    The exact equivalent gets a little bit complicated:

    class A
    {
        std::shared_ptr<B> b;
        //some other stuff
    
    public:
        A(std::shared_ptr<B> const & b) : b(b) {}
    };
    
    class B : public std::enable_shared_from_this<B>
    {
    public:
        A createA() {return A(shared_from_this());}
    };
    
    A complicatedCreateA()
    {
        std::shared_ptr<B> b = std::make_shared<B>();
        return b->createA();
    }
    

    Alternatively, you could avoid the shared_from_this malarkey by making createA a non-member (or static member, if it needs access to Bs privates), taking a shared pointer argument:

    A createA(std::shared_ptr<B> const & b) {return A(b);}
    
    A complicatedCreateA()
    {
        std::shared_ptr<B> b = std::make_shared<B>();
        return createA(b);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following classes: class A { public string Test {get; set;} } class
Imagine the following two entities. Element is simple class containing some data: @Entity public
Imagine the following: class Repository { private ObservableCollection<ModelClass> _allEntries; public ObservableCollection<ModelClass> AllEntries { get
I didn't imagine that I would encounter radically new syntax in Java anymore at
Imagine the following two classes of a chess game: TChessBoard = class private FBoard
Imagine the following code: class SimpleLetter def values (a .. z).to_a end def ===(other)
Imagine the following class that displays some sort of hierarchy: class BaseList2D(object): def __init__(self):
Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
Imagine the following class: class A { public event EventHandler AnyEvent; } You create
Imagine we have following classes: public interface MyInterface<T> { List<T> getList(T t); } abstract

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.