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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:25:52+00:00 2026-05-24T01:25:52+00:00

I have a class A which has an reference to an object of class

  • 0

I have a class A which has an reference to an object of class B as a member. The copy constructor (and assignment operator) of class B is private. Do you think it is a valid and good idea to use
the default copy constructor for A. (I actually want a functionality where I can store a lots of object for type A in some sort of an STL Container which requires both assign-ability and copy-ability.)

class A
{
    private:
        B& b_;

    public:
        A(B& b) : b_(b){}
}

Till now, in my knowledge the objections to above method are the following but my design does not face it. I would like to know if there are some other problems/issues/concerns about the above example…

  1. Only the reference is copied and hence, there would be problems when the original object b of type B is destroyed. (Does not apply, because b is available throughout the entire scope.)
  2. Is b_ unique for every instance of A? (No, B is actually instantiated only once in the scope, so it has the effect of a singleton class.)

If there are other concerns, please list them here. I am not keen on a explicitly defined copy-constructor but i’m keeping an open mind towards it.

  • 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-24T01:25:54+00:00Added an answer on May 24, 2026 at 1:25 am

    As a general guideline, I never store references inside objects because I cannot get copy semantics for free.

    I store pointers instead. Here, storing a dumb pointer and letting the compiler implement copy semantics for you seems fine:

    class A
    {
        B* b; // or a smart pointer, depending on what semantics you want.
    
    public:
        A(B& b) : b(&b) {}
    };
    

    Using a pointer has some flexibility: for instance, default construction can set the pointer to zero, and subsequent operations check against its validity (or simply assert). Also, the pointer can be reset, which is not the case with a reference.

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

Sidebar

Related Questions

I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class ReportingComponent<T> , which has the constructor: public ReportingComponent(IQueryable<T> query) {}
Let's say I have a class that has a member called data which is
For my exception class i have a constructor that has multi arguments (...) which
I have a class which has many small functions. By small functions, I mean
I have an class which has a enum property and a boolean property, based
I have a class which represents a shape. The Shape class has a property
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
I have a component which has a List<T> property. The class in the list
I have a base class Foo that has an Update() function, which I want

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.