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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:12:32+00:00 2026-05-26T14:12:32+00:00

Given a simple class MyClass with a constructor that accepts two int , how

  • 0

Given a simple class MyClass with a constructor that accepts two int, how can I initialize an array of MyClass in the heap?

I’ve tried

MyClass *classes[2] = { new MyClass(1, 2),
                        new MyClass(1, 2) };

But this doesn’t seem to work. Thanks

  • 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-26T14:12:32+00:00Added an answer on May 26, 2026 at 2:12 pm

    Use the std::allocator<MyClass> for this.

    std::allocator<MyClass> alloc;
    MyClass* ptr = alloc.allocate(2);  //allocate
    for(int i=0; i<2; ++i) {
        alloc.construct(ptr+i, MyClass(1, i)); //construct in C++03
        //alloc.construct(ptr+i, 1, i); //construct in C++11
    }
    
    //use
    
    for(int i=0; i<2; ++i) {
        alloc.destroy(ptr+i); //destruct
    }
    alloc.deallocate(ptr); //deallocate
    

    Note that you don’t have to construct all that you allocate.

    Or, better yet, just use std::vector.

    [EDIT]
    KerrekSB suggested this as simpler:

    MyClass** ptr = new MyClass*[3];
    for(int i=0; i<4; ++i)
        ptr[i] = new MyClass(1, i);
    
    //use
    
    for(int i=0; i<4; ++i)
       delete ptr[i];
    delete[] ptr;
    

    It’s slightly slower access, but much easier to use.

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

Sidebar

Related Questions

This is a purely theoretical question. Given three simple classes: class Base { }
Given a simple class that overloads the '[ ]' operator: class A { public:
My question is rather simple, given: class MyClass{ function a(){ echo F.A ; }
Given that I have a class Base that has a single argument constructor with
Given a simple class: public class Person { public string FirstName; public string LastName;
Given a simple C# class definition like: [System.Windows.Markup.ContentProperty(PropertyOne)] public class SimpleBase { public string
Given a simple parent/child class structure. I want to use linqkit to apply a
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
Given a simple switch statement switch (int) { case 1 : { printf(1\n); break;
Here is my class definition: class MyClass { public: void test(int val = 0);

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.