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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:36:51+00:00 2026-06-14T20:36:51+00:00

Consider the following code: class A { private: struct B { private: int i;

  • 0

Consider the following code:

class A
{
private:
    struct B { private: int i; friend class A; };

public:
    static void foo1()
    {
        B b;
        b.i = 0;
    }

    static void foo2()
    {
        B b = {0};
    }
};

Why does foo1 work but not foo2? Is not the struct initializer constructor visible for class A? Is there anyway to make this work in C++11?

(Note, removing the private makes foo2 working.)

  • 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-14T20:36:53+00:00Added an answer on June 14, 2026 at 8:36 pm

    Why does foo1 work but not foo2? Is not the struct initializer constructor visible for class A?

    B b = {0};
    

    Does not work because B is not an Aggregate. And it is not an Aggregate because it has an non-static private data member. If you remove the private specifier, B becomes an Aggregate and hence can be initialized in this manner.


    C++03 Standard 8.5.1 Aggregates
    Para 7:

    If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized (8.5).
    [Example:

     struct S { int a; char* b; int c; };
     S ss = { 1, "asdf" };
    

    initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is,0. ]

    C++03 standard 8.5.1 §1:

    An aggregate is an array or a class (clause 9) with no user-declared
    constructors (12.1), no private or protected non-static data members (clause 11),
    no base classes (clause 10), and no virtual functions (10.3).

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

Sidebar

Related Questions

Consider the following code: class A { public: virtual void f() throw ( int
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Consider the following code: public class Foo { private static final Object LOCK =
consider the following code: public class SynchronizedCounter extends Thread { private int c =
Consider the following test 1 code struct A { private: class face; friend class
Consider the following piece of code - class MyThread extends Thread { private int
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code snippet within a class private static Object _syncroot = new
Consider the following code: class Rectangle { public: // Constructors Rectangle(){ init(0,0); } Rectangle(int
Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;

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.