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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:30:43+00:00 2026-05-14T04:30:43+00:00

Why do I have to provide default ctor if I want to create an

  • 0

Why do I have to provide default ctor if I want to create an array of objects of my type?
Thanks for answers

  • 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-14T04:30:44+00:00Added an answer on May 14, 2026 at 4:30 am

    Because they have to be initialized.

    Consider if it wasn’t the case:

    struct foo
    {
        foo(int) {}
    
        void bar(void) {}
    };
    
    foo a[10];
    foo f = a[0]; // not default-constructed == not initialized == undefined behavior
    

    Note you don’t have to:

    int main(){
       // initializes with the int constructor
       foo a[] = {1, 2, 3};
    }
    
    // if the constructor had been explicit
    int main(){
       // requires copy-constructor
       foo a[] = {foo(1), foo(2), foo(3)};
    }
    

    If you really need an array of objects and you can’t give a meaningful default constructor, use std::vector.

    If you really need an array of of objects, can’t give a meaningful default constructor, and want to stay on the stack, you need to lazily initialize the objects. I have written such a utility class. (You would use the second version, the first uses dynamic memory allocation.)

    For example:

    typedef lazy_object_stack<foo> lazy_foo;
    
    lazy_foo a[10]; // 10 lazy foo's
    
    for (size_t i = 0; i < 10; ++i)
    {
        // create a foo, on the stack, passing `i` to the constructor
        a[i].create(i); 
    }
    
    for (size_t i = 0; i < 10; ++i)
        a[i].get().bar(); // and use it
    
    // automatically destructed, of course
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface IFoo for which I want to provide a default implementation
I'm working on a resource management class and want to have the user provide
I have a helper method 'default_content_for' which can be used to provide default content
I have to confess that I hate membership provider. The default implementation is not
Lets say I have to provide an value as bitmask. NSUInteger options = kFoo
Do the major web application frameworks (Rails, Django, etc) have libraries that provide functionality
I have been asked to provide information on available techniques for assessing our current,
I have a class that provide the CRUD operation for an entity.Im using the
I have a requirement to be able to provide a flex component in English
Have any bridge libraries been developed for PHP that provide access to the jQuery

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.