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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:27:29+00:00 2026-05-28T02:27:29+00:00

Given the code sample: class B { //Some contents. }; class C { private:

  • 0

Given the code sample:

class B {
    //Some contents.
};

class C {
    private:
        B& b;
};

class A {
    private:
        B b;
        C c;
};

Class C has a reference to a b, so it needs to be initialized with it. Class A contains an instance of B and an instance of C.

My question is: Can I initialize the C instance in A with the B instance in A (assuming I did bother to put the constructors in)? Secondly, do I need to perform any explicit initialization of the B in A, or is it default initialized since its a class type within a class?

  • 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-28T02:27:30+00:00Added an answer on May 28, 2026 at 2:27 am

    Member variables are initialised in the order that they are declared in the class declaration (even if you have them in a different order in the initialisation list of the constructor), so yes, by the time c is being initialised, b will be initialised, and you can use b to initialise c.

    As Ricardo Cardenes notes, it will still work even if you declare c before b in the class definition (which means you will pass C::C a reference to an uninitialised B) however you cause undefined behaviour if you use the object inside C::C. It’s safer to declare b first, because although you may not use b inside C::C now, you might in the future and forget that the reference refers to an uninitialised B, and cause UB.

    And no, you do not have to explicitly initialise b (unless it is POD) unless you don’t want it to be default-constructed. So this code would be what you want (again, if B isn’t POD):

    A::A() : c(b) { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I am studying some sample code given in my web dev class as
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Given the following code sample: uint8_t i, in, ni; i = in = 2;
Can anybody give me some sample source code showing how to connect to a
I have defined some models like this (Entity Framework Code-First): public class A {
In the sample code, the line with the 'error comment' gives the following the
I've worked on many projects where I've been given code by others to update.
Given the code from the Complex Form part III how would you go about
Given the code bellow, how do I style the radio buttons to be next
I am wondering if I am able to do this in Vim: Sample code:

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.