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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:43:30+00:00 2026-06-16T03:43:30+00:00

often I encounter code like /*initializer list of some class*/:m_member(some_param,/* –> */ *this) Reason

  • 0

often I encounter code like

/*initializer list of some class*/:m_member(some_param,/* --> */ *this)

Reason why this is done is so that m_member can call member functions from the class that contains it…
aka

//code in class that is m_member instance of

    m_parent->some_function();

I personally dislike it because I consider it pathetic design(“dear child do you know what are you doing to your class encapsulation”), but I would like to know is in general this behavior bad, and if so how to avoid this kind of design.

EDIT: please dont focus on this in initalizer list, lets say it is in ctor body.

  • 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-16T03:43:32+00:00Added an answer on June 16, 2026 at 3:43 am

    It can be disastrous, since your parent is not constructed a the time of the reference-set. The following example will demonstrate this:

    #include <iostream>
    using namespace std;
    
    struct TheParent;
    
    struct TheChild
    {
        TheChild(TheParent& parent);
        TheParent& myParent;
    };
    
    struct TheParent
    {
        TheParent()
          : mychild(*this)
          , value(1)
        {
            cout << "TheParent::TheParent() : " << value << endl;
        }
    
        TheChild mychild;
        int value;
    };
    
    TheChild::TheChild(TheParent& parent)
       : myParent(parent)
    {
        cout << "TheChild::TheChild() : " << myParent.value << endl;
    };
    
    int main()
    {
        TheParent parent;
        return 0;
    }
    

    Produces the following output, clearly noting the indeterminate state of the parent object:

    TheChild::TheChild() : 1606422622
    TheParent::TheParent() : 1
    

    Bottom line: don’t do it this way. You would be better served to use a dynamic child allocation instead, but even this has caveats:

    #include <iostream>
    using namespace std;
    
    struct TheParent;
    
    struct TheChild
    {
        TheChild(TheParent& parent);
        TheParent& myParent;
    };
    
    struct TheParent
    {
        TheParent()
          : mychild(NULL)
          , value(1)
        {
            mychild = new TheChild(*this);
            cout << "TheParent::TheParent() : " << value << endl;
        }
    
        ~TheParent()
        {
            delete mychild;
        }
    
        TheChild* mychild;
        int value;
    };
    
    TheChild::TheChild(TheParent& parent)
       : myParent(parent)
    {
        cout << "TheChild::TheChild() : " << myParent.value << endl;
    };
    
    
    int main()
    {
        TheParent parent;
        return 0;
    }
    

    This give you what you’re likely hoping for:

    TheChild::TheChild() : 1
    TheParent::TheParent() : 1
    

    Note, however, even this has issues if TheParent is an intermediate class in an inheritance chain, and you’re desiring to access potentially overridden virtual implementations of functions in derived classes that have yet to be constructed.

    Again, bottom line, if you find yourself doing this, you may want to think about why you need to in the first place.

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

Sidebar

Related Questions

Couple of examples that I encounter quite often. Indenting some code E.g. I want
Often times I write some SQL like this: string sql = @ -- Multi-line
Often when writing PHP I'll have it output some HTML like this - echo
I'm currently doing some oracle sql analysis and often encounter valid sql statement that
We all encounter and hear them more often lately and i'd like some good
Recently, I often encounter errors like this: Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil) I
This is a problem we encounter often. There must be some best practices to
Looking back at my past projects I often encounter this one: A client or
I often have to implement some interfaces such as IEnumerable<T> in my code. Each
I often encounter situations in editing text or code where I want to insert

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.