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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:06:49+00:00 2026-06-10T16:06:49+00:00

I have one instance of Class A. It has two members, Object A and

  • 0

I have one instance of Class A. It has two members, Object A and Object B. I need to create ONE instance of Object A (which is quite a large object), and TWO instances of object B, and then give both instances of object B access to that object A. They don’t ever need to change object A, they just need to access its info.

Right now, I got it all to work by creating Object A as a pointer, passing it into both instances of Object B, who have their own pointer that then points to the same place in memory as Object A’s pointer. This works fine, but I think that’s a pretty big No-No right? Because once Class A deletes the original pointer I’ll have some dangling pointers, right? Is there a better way to do this?

(FYI – Object A takes a couple seconds to load at a time in the program where I need a fast load time, that’s why I have to create only one instance and pass it to both of Object Bs instead of letting Object B create their own instances of Object A. )

  • 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-10T16:06:51+00:00Added an answer on June 10, 2026 at 4:06 pm

    If I follow you correctly, you want something like this:

    // Class for "Object A" in your question
    class Foo
    {
        // ...
    };
    
    // Class for "Object B" in your question
    class Bar
    {
        Foo &foo_;
    
    public:
        Bar(Foo &foo)
            : foo_(foo)
            {}
    };
    
    class A
    {
        Foo foo_;
        Bar bar1_;
        Bar bar2_;
    
    public:
        A()
            : bar1_(foo_),
              bar2_(foo_)
            {}
    };
    

    Using no pointers, no allocations, so no dangling pointers/references or memory leaks. When A gets destroyed so does bar1_ and bar2_ too along with foo_, so the two instances of Bar will not be left with any dangling references either.

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

Sidebar

Related Questions

I have two instances of VS2010 running on same machine.One VS instance has a
I have only one class with many instances. Every instance is observer of couple
We have a weblogic server 10.0 instance which has a cluster with one managed
I have two classes, Car and Person. Car has as one of its members
I have a class that has two method in it, one calls a class
I have an abstract test class that has two type parameters. I need to
Suppose I have two instances of the same class. The class has a pointer
I have a Node class that has an 'element' slot which contains a list
I have custom-made wizard system which so far has been quite suitable. For most
I have a class CommonTableModel that has several instance methods and each operate 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.