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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:56:01+00:00 2026-05-13T20:56:01+00:00

I have: class Foo; class Bar { Foo foo; Bar(): foo(foo) {}; } Bar

  • 0

I have:

class Foo;

class Bar {
  Foo foo;
  Bar(): foo(foo) {};
}

Bar bar;

At this point, is

bar.foo // <--- how is this initialized?

[This question arose from a buggy ref-counted pointer implemntation; I could have sworn that I ensured each pointer was pointing at something non-null; but I ended up with a pointer that pointed at something NULL.]

  • 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-13T20:56:02+00:00Added an answer on May 13, 2026 at 8:56 pm

    foo is fully initialized once you’ve entered the body of the constructor (that’s the guaranteed general case; specifically once it has finished initializing in the initialize list.)

    In your case, you are copy-constructing from a non-constructed object. This results in undefined behavior, per §12.7/1 (thank you, gf):

    For an object of non-POD class type (clause 9), before the constructor begins execution and after the destructor finishes execution, referring to any nonstatic member or base class of the object results in undefined behavior.

    In fact, it gives this example:

    struct W { int j; };
    struct X : public virtual W { };
    struct Y {
        int *p;
        X x;
        Y() : p(&x.j) // undefined, x is not yet constructed
        { }
    };
    

    Note, the compiler is not required to give a diagnosis of undefined behavior, per §1.4/1. While I think we all agree it would be nice, it simply isn’t something the compiler implementers need to worry about.


    Charles points out a loophole of sorts. If Bar has static storage and if Foo is a POD type, then it will be initialized when this code runs. Static-stored variables are zero-initialized before an other initialization runs.

    This means whatever Foo is, as long as it doesn’t need a constructor to be run to be initialized (i.e., be POD) it’s members will be zero-initialized. Essentially, you’ll be copying a zero-initialized object.

    In general though, such code is to be avoided. 🙂

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

Sidebar

Related Questions

I have the following member of class foo. foo &foo::bar() { return this; }
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
I have a relationship like this class Foo { static hasMany = [bars: Bar,
I have class Foo(): function bar(): pass function foobar(): pass Rather than executing each
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have: class Foo { public String Bar { get; set; } }
Suppose I have: class Foo { ... }; class Bar : public Foo {
Suppose I have the following C++ class: class Foo { double bar(double sth); };
I have: class Foo(models.Model): pass class Bar(Foo): pass class Corn(Foo): pass # Now I

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.