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

The Archive Base Latest Questions

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

Comming from Java, I have difficulty with the code below. In my understanding b

  • 0

Comming from Java, I have difficulty with the code below.
In my understanding b is just declared on line 3 but not instantiated.

What would be the text book way of creating an instance of B in class A?

class A {
  private:
    B b;
  public:
    A() { 
      //instantiate b here?
    }
};

Edit: What if B does not have a default constructor?

  • 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:22:54+00:00Added an answer on June 8, 2026 at 11:22 am

    You could explicitly initialize b in A‘s constructor’s initialization list, for example

    class A {
      B b; // private
     public:
      A : b() {} // the compiler provides the equivalent of this if you don't
    }; 
    

    However, b would get instantiated automatically anyway. The above makes sense if you need to build a B with a non-default constructor, or if B cannot be default initialized:

    class A {
      B b; // private
     public:
      A : b(someParam) {}
    };
    

    It may be impossible to correctly initialize in the constructor’s initialization list, in which case an assignment can be done in the body of the constructor:

    class A {
      B b; // private
     public:
      A {
        b = somethingComplicated...; // assigns new value to default constructed B.
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just 'inherited' a Java-project and not coming from a Java-background I am
I have a good understanding of OO from java and C# and I'm lucky
I'm comming from the java world, and i'd like to know if there's anything
Background: I'm coming from Java background so not too familiar with Javascript. We are
I just started learning C++ (coming from Java ) and am having some serious
I have just started to learn the very basics of Java programming. Using a
Coming from Java background I am guessing this is expected. I would really love
Coming from Java and Python, I am not so well versed in memory management,
I have to improve some code where an Oracle stored procedure is called from
I have a XML string comming from a Rest WS that I created 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.