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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:08:33+00:00 2026-05-29T21:08:33+00:00

I need to initialize an array of objects of a parametrized constructor . How

  • 0

I need to initialize an array of objects of a parametrized constructor . How can i do it in best possible way ?

   # include<iostream>
   # include<conio.h>
   # include<stdio.h>

   using namespace std;

   class A
   {
    public:
    int a;       
    A();
    A(int x)
    {
     a=x;
    }      
   };
   int main()
   {
    A *a1,a2(1);
    a1 = (A*)malloc(sizeof(A)*10); // equivalent to A[10]. 
    for(int i=0;i<10;i++) a1[i]=a2; // Initialization is important in any program.
    for(int i=0;i<10;i++) cout<<a1[i].a; 
    getch(); 
    return 0;   
   }

This does work but is there some other way better than this ?

  • 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-29T21:08:34+00:00Added an answer on May 29, 2026 at 9:08 pm

    The C++ way would be to use a std::vector.

    std::vector<A>   a1(10, 1);
    

    creates 10 A‘s initialized by 1.

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

Sidebar

Related Questions

I need to initialize few objects, it can take a while so I want
I have a class where I need the constructor to initialize variable array. I
I need to return an array of initialized objects from VB6 into C# using
To initialize an int array with all zeros, do I need to use: int
I need a 'good' way to initialize the pseudo-random number generator in C++. I've
I need a dynamically allocated bi-dimensional array of objects. I declared a static pointer
Hey guys I need to know who to put these array objects into two
I am trying to initialize an array of objects: SinglyLinkedList offeredClasses[22] = {SinglyLinkedList(CSCE101),SinglyLinkedList(CSCE101L),SinglyLinkedList(CSCE150E),SinglyLinkedList(CSCE150EL),SinglyLinkedList(CSCE150EM),SinglyLinkedList(CSCE150EML),SinglyLinkedList(CSCE155),SinglyLinkedList(CSCE155H),SinglyLinkedList(CSCE156),SinglyLinkedList(CSCE230),SinglyLinkedList(CSCE230L),SinglyLinkedList(CSCE235),SinglyLinkedList(CSCE251),SinglyLinkedList(CSCE310),SinglyLinkedList(CSCE322),SinglyLinkedList(CSCE361),SinglyLinkedList(CSCE351),SinglyLinkedList(CSCE451),SinglyLinkedList(CSCE423),SinglyLinkedList(CSCE428),SinglyLinkedList(CSCE486),SinglyLinkedList(CSCE487)}; but
Is there way to initialize a numpy array of a shape and add to
I need to initialize a session var as array. Where i do that? when

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.