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

  • Home
  • SEARCH
  • 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 547333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:57:43+00:00 2026-05-13T10:57:43+00:00

I was just wondering, whether an array member of a class could be created

  • 0

I was just wondering, whether an array member of a class could be created immediately upon class construction:

class C
{
      public:
          C(int a) : i(a) {}

      private:
          int i;
};

class D
{
 public:
        D() : a(5, 8) {}
        D(int m, int n) : a(m,n) {}

     private:
     C a[2];

};

As far as I have googled, array creation within Constructor such as above is in C++ impossible. Alternatively, the array member can be initialized within the Constructor block as follows.

class D
    {
     public:
         D() { 
               a[0] = 5;
               a[1] = 8; 
             }
         D(int m, int n) { 
                           a[0] = m;
                           a[1] = n; 
                         }
         private:
         C a[2];

    };

But then, It is not an array creation anymore, but array assignment. The array elemnts are created automatically by compiler via their default constructor and subsequently they are manually assigned to specific values within the C’tor block. What is annoying; for such a workaround, the class C has to offer a default Constructor.

Has anybody any idea which can help me in creating array members upon construction. I know that using std::vector might be a solution but, due to project conditions I am not allowed to use any standard, Boost or third party library.

  • 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-13T10:57:44+00:00Added an answer on May 13, 2026 at 10:57 am

    Arrays — a concept older than C++ itself, inherited straight from C — don’t really have usable constructors, as you’re basically noticing. There are few workaround that are left to you given the weird constraints you’re mentioning (no standard library?!?!?) — you could have a be a pointer to C rather than an array of C, allocate raw memory to it, then initialize each member with “placement new” (that works around the issue of C not having a default constructor, at least).

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

Sidebar

Related Questions

I have created a HyperSQL Database. I was just wondering whether I could run
I was just wondering whether there is some way to do this: I have
I've come across a Registry Class and I'm wondering whether to bother with this
Just wondering whether an object can self-destruct. Consider this situation. An object that extends
Just wondering whether an object can self-destruct. Consider this situation. An object that extends
I was just wondering whether we actually need the algorithm to be muti-threaded if
Just wondering whether anyone knows what events an HTML5 <input type=number /> element fires
Im just wondering whether there is a way to call a method where i
I was just wondering whether if it was possible to replace recursion with an
Hey all, just wondering whether the following would cause a memory leak? char* a

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.