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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:54:08+00:00 2026-05-10T15:54:08+00:00

In C++, it’s not possible to initialize array members in the initialization list, thus

  • 0

In C++, it’s not possible to initialize array members in the initialization list, thus member objects should have default constructors and they should be properly initialized in the constructor. Is there any (reasonable) workaround for this apart from not using arrays?

[Anything that can be initialized using only the initialization list is in our application far preferable to using the constructor, as that data can be allocated and initialized by the compiler and linker, and every CPU clock cycle counts, even before main. However, it is not always possible to have a default constructor for every class, and besides, reinitializing the data again in the constructor rather defeats the purpose anyway.]

E.g. I’d like to have something like this (but this one doesn’t work):

class OtherClass { private:     int data; public:     OtherClass(int i) : data(i) {}; // No default constructor! };  class Foo { private:     OtherClass inst[3]; // Array size fixed and known ahead of time. public:     Foo(...)         : inst[0](0), inst[1](1), inst[2](2)         {}; }; 

The only workaround I’m aware of is the non-array one:

class Foo { private:     OtherClass inst0;     OtherClass inst1;     OtherClass inst2;     OtherClass *inst[3]; public:     Foo(...)         : inst0(0), inst1(1), inst2(2) {         inst[0]=&inst0;         inst[1]=&inst1;         inst[2]=&inst2;     }; }; 

Edit: It should be stressed that OtherClass has no default constructor, and that it is very desirable to have the linker be able to allocate any memory needed (one or more static instances of Foo will be created), using the heap is essentially verboten. I’ve updated the examples above to highlight the first point.

  • 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. 2026-05-10T15:54:09+00:00Added an answer on May 10, 2026 at 3:54 pm

    One possible workaround is to avoid the compiler calling the OtherClass constructor at all, and to call it on your own using placement new to initialize it whichever way you need. Example:

      class Foo   {   private:     char inst[3*sizeof(OtherClass)]; // Array size fixed. OtherClass has no default ctor.      // use Inst to access, not inst     OtherClass &Inst(int i) {return (OtherClass *)inst+i;}     const OtherClass &Inst(int i) const {return (const OtherClass *)inst+i;}   public:     Foo(...)     {       new (Inst(0)) OtherClass(...);       new (Inst(1)) OtherClass(...);       new (Inst(2)) OtherClass(...);     }     ~Foo()     {       Inst(0)->~OtherClass();       Inst(1)->~OtherClass();       Inst(2)->~OtherClass();     }   }; 

    To cater for possible alignment requirements of the OtherClass, you may need to use __declspec(align(x)) if working in VisualC++, or to use a type other than char like:

    Type inst[3*(sizeof(OtherClass)+sizeof(Type)-1)/sizeof(Type)]; 

    … where Type is int, double, long long, or whatever describes the alignment requirements.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Don't do this: <input type="checkbox" name="del_#ItemHasUsers[i]["ID"]#"> Do this: <input type="checkbox"… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer Have you looked at Lucene? It's open-source and the Java… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer One "normal" way would be to create a new Bitmap… May 11, 2026 at 11:52 pm

Related Questions

In C++, it's not possible to initialize array members in the initialization list, thus
In C++, I understand that it is possible to create a Singleton base class
In C++, it is legal to give an implementation of a pure virtual function:
In C++, when is it best to use the stack? When is it best
In C++, is it possible to have a base plus derived class implement a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.