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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:41:25+00:00 2026-06-12T21:41:25+00:00

Please consider the following example struct Foo { int bar; Foo(int i):bar(i){cout << real

  • 0

Please consider the following example

struct Foo
{
    int bar;    
    Foo(int i):bar(i){cout << "real ctor\n";}   
    Foo(){cout << "default ctor\n";}
};

int main()
{   
    Foo fooArr[3];//default ctor called 3 times 
    for(int i=0;i!=3;++i)cout << fooArr[i].bar << endl;//bare memory junk
    cout << endl;

    vector<Foo> fooVec;
    for(int i=0;i!=3;++i){
        fooVec.push_back(Foo(i));     //only real ctor called
        cout << fooVec[i].bar << endl;//real thing 
    }
    cout << endl;

    int iArr[3];
    for(int i=0;i!=3;++i)cout << iArr[i] << endl;//bare memory junk
}

I don’t want any user of Foo to call its default constructor, because it’s not in my design. But I’d like my users to be able to use an array of Foo, to support that, I was forced to provide a pointless and confusing Foo::Foo(). I just don’t understand why does the C++ standard force programmers to do such a thing. What is the rationale behind it? Why the inconsistency? Could any of you smart guys who get this explain it to me, please? Thanks in advance!

  • 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-12T21:41:26+00:00Added an answer on June 12, 2026 at 9:41 pm

    You can make arrays of Foo even if it doesn’t have a default constructor. It’s just that the elements have to be constructed when you declare the array. So you can do this:

    Foo fooArr[] = { Foo( 1 ), Foo( 2 ), Foo( 3 ) };
    

    The alternative is to use a a dynamic array (your vector<Foo> example, which is probably best) or an array of pointers to Foo (like shared_ptr<Foo> arrFoo[3])

    shared_ptr<Foo> arrFoo[3];
    arrFoo[2].reset( new Foo(3) );
    

    A final note about vector<Foo>: since the size of the array is known in advance, you can improve performance by reserving enough space in the vector for all future Foos:

    vector<Foo> arrFoo;
    arrFoo.reserve( 3 );
    
    for( int i = 0; i<3; ++i )
        arrFoo.push_back( Foo( i ) );
    

    EDIT: Your question was why do you have to have a default constructor to make a static array of the type. I thought the answer was clear but I’ll try to explain it.

    Foo bar1; Foo bar2; creates two objects using the default constructor, since no arguments were provided.

    Foo bar[2]; is essentially the same thing. It declares two objects that need to be constructed. There is no way to declare an object without constructing it – that’s the very point of declaring it in the first place.

    A static array in C++ is just a bunch of objects placed contiguously memory. It’s not a separate object.

    Hope that makes sense.

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

Sidebar

Related Questions

Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
Please consider the following example def foo(a: Int, b: Int = 100) = a
Please consider the following simple use case: public class Foo { public virtual int
please consider the following code: template <typename T> struct foo { template <typename S>
Let us consider the following factorial example : #include <iostream.h> int factorial(int); void main(void)
Please consider the following example: class Example { int &_m; public: /** * An
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
Please consider the following context from Innate : # Default application for Innate def
Please consider the following SQL Server table and stored procedure. create table customers(cusnum int,
Consider the following code template<typename T, int N> struct A { typedef T value_type;

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.