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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:56:02+00:00 2026-05-30T20:56:02+00:00

In cpp, is this or something equivalent possible? Foo bar[23] = Foo(); EDIT: The

  • 0

In cpp, is this or something equivalent possible?

Foo bar[23] = Foo();

EDIT:

The motivation for the question was that I think I saw somebody using this syntax

vtkSmartPointer<Foo> bar[23] = vtkSmartPointer<Foo>::New();

and wondered why it compiles and how many new objects are actually created…

  • 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-30T20:56:03+00:00Added an answer on May 30, 2026 at 8:56 pm

    Not with this syntax, but if Foo has a non-trivial default
    constructor,

    Foo bar[23];
    

    will call it for each member of the array. More generally, you can also
    write:

    Foo bar[23] = { x, y, z... };
    

    The compiler will try to convert each initializer (which can be an
    arbitrary expression) into a Foo, and use that to initialize the
    element of the array. If there are not enough initializer expressions,
    then all of the following elements will be initialized with Foo().

    EDIT:

    Since several comments asked for it: if Foo doesn’t have a user defined
    constructor, the situation changes (since calling the “constructor”
    won’t do anything). In that case, the behavior of:

    Foo bar[23];
    

    depends on the variables lifetime: if it has static lifetime, it will be
    zero initialized; otherwise, it won’t be initialized at all. In either
    case, you can use aggregate initialization to force the initialization
    you want:

    Foo bar[23] = { { firstMember, secondMember... }, ... };
    

    If there aren’t enough initializers, the remaining elements are zero
    initialized, so:

    Foo bar[23] = {};
    

    will zero initialize all of the members.

    For completeness, I should point out that aggregate initialization
    cannot be used for class members: the only ways you can initialize a C
    style array member is by means of assignment to each element, in the
    body of the constructor, or by copy initialization: define a static
    Foo and initialize the member with it.

    I should probably also point out that all of the above refers to C++03.
    C++11 introduced an extended initialization syntax; in particular, you
    can use something that looks like aggregate initialization for class
    members as well. (I think—I’m not too familiar with C++11, as not
    all of my compilers support it yet.)

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

Sidebar

Related Questions

My makefile looks something like this: FOO_OBJECT_FILES := $(OBJDIR)/Foo.cpp.o BAR_OBJECT_FILES := $(OBJDIR)/Bar.cpp.o $(OBJDIR)Bar.c.o ALL_OBJECT_FILES
I was looking over some (C++) code and found something like this: //Foo.cpp namespace
I occasionally write code something like this: // file1.cpp #define DO_THIS 1 #if DO_THIS
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
In C++, I code this way: //foo.h class cBar { void foobar(); } //foo.cpp
How would I best implement these? I thought of something like this: using namespace
Suppose I want something of this sort, in one .cpp source file: class A
I have a (partial) qmake project file like this: TEMPLATE=lib TARGET=whatever SOURCES=whatever.cpp HEADERS=whatever.h This
This is one of my most dreaded C/C++ compiler errors: file.cpp(3124) : fatal error
I am getting this compiler (gcc 4.5) warning: Formatter.cpp:23: warning: unknown conversion type character

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.