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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:01:11+00:00 2026-06-01T19:01:11+00:00

Is it okay to pass the reference to an object (of type) Container in

  • 0

Is it okay to pass the reference to an object (of type) Container in the member initialization list of its constructor in order to initialize a member of Container as follows:
(code on ideone).

#include <cstdlib>
#include <iostream>

struct Container;

struct Member
{
    Member( Container& container ) : m_container( container )
    {
    }

    Container& m_container;
};

struct Container
{
    Container() : m_member( *this )
    {
    }

    Member m_member;
};

int main()
{
    Container c;
    return EXIT_SUCCESS;
}

The code compiles but I’m not sure if its standard.

  • 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-01T19:01:13+00:00Added an answer on June 1, 2026 at 7:01 pm

    That’s fine; the member reference is initialised to refer to the object that was passed as an argument.

    However, since the Container is still under construction, you mustn’t access it in that constructor; the only thing you can do with the reference is initialise another reference.

    You must also make sure that you don’t use that reference after the container is destroyed. In this example, it’s fine – m_member, and the reference it contains, are destroyed along with the container.

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

Sidebar

Related Questions

Okay, consider the following classes: class Object { public: // Constructor Object() : [Initialization
Okay, so the book says: when creating a new object, you can pass a
I have a function function callback(obj){...} Is it okay to pass in more objects
Okay, so I am basically making a script to pass post data using cURL.
Okay, so I know how to get a list of accounts so that I
Okay, I actually don't have code as of yet because i'm just picking out
Okay so I'm trying to pass elements of a 2D array with string elements
Okay, I just directly put my code here: main.cpp mysqlpp::Connection conn(false); int main() {
Okay, so I have two classes, call them A and B--in that order in
We can pass reference of an array to a function like: void f(int (&a)[5]);

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.