Problems with invoking constructor of dynamic allocated array

hello everyone
here is my problem that I encountered when programming c++ in SDL,
but this problem is just a very simple c++ problem:

I have already made a button class with constructor(has parameter).
and then attached that class into my game class, which is a pointer.

CButton *pBtn; // in the CGame class

so when the game class initiates, I use dynamic allocated (new) to construct an array of button object, as I need 4 buttons to represent each one in the menu.

The code is like this:

1
2
3
4
void CGame::Init()
{
pBtn=new Cbutton[4];
}


The compiler does not pass because the object does not call the constructor. So I was wondering how to invoke the CButton's constructor

Thanks in advance!
Last edited on
The Cbutton's constructor runs automatically when the dynamic instance of the object is created using the new keyword.

You can test this out easily - just have the Cbutton constructor output a debug statement letting you know it ran. Then you can see where in the code it runs.
Thanks for your reply, but that is not actually what I mean
I have 4 parameters to pass:

CButtom(int posX,int posY, int w, int h) {x=posX; y=posY; width=w; height=h;}

so how could I pass these parameter to the constructor with the dynamically allocated objects?
Last edited on
Well, I don't know of a fast way to do it, but sure this will work:
1
2
3
4
5
6
7
8
9
/* In the class */
CButton ** pBtn; // Note: It's a Pointer to Pointer to CButton!
/* In the function declarations */
void CGame::Init(/*Parameters*/)
{
   pBtn = new CButton* [4]; // Declare 4 Pointers to CButton
   for(unsigned int i = 0; i < 4; i++)
      pBtn[i] = new CButton(/*Parameters*/); // Each of the 4 Pointers to CButton now will point to a CButton
}

Just remember to first destroy each of the 4 buttons, then destroy the pBtn like this:
1
2
3
4
5
6
7
8
9
/* In the class */
unsigned int pBtnSize; // Set this to the allocated size
/* The Destroy Function */
void CGame::Destroy()
{
   for(unsigned int i = 0; i < pBtnSize; i++)
      delete pBtn[i];
   delete[] pBtn;
}
Last edited on
The problem here isn't so much to do with the dynamic allocation as it is the pluralisation of object.

Dynamically invoking a non-default constructor is a simple as a static instance:
1
2
CButton *ptr;
ptr = new CButton(1,2,3,4);


The problem here is that you have a pointer to an array. You can't specify what to do with each item at the point of instantiation, so the compiler is looking for the default constructor, which doesn't exist.

I think the best solution is to write a new default constructor. Then write a Set method to set the values.

1
2
3
4
5
6
7
class CButton
{
private:
   // Member vars here
public:
   CButton():x(0),y(0),width(0),height(0){}
};


That constructor will run when the dynamic instances are created, since you don't specify parameters. Remember that as soon as you write your own constructor, the default one is no longer available.

You can then set each one using a Set method, which will do more or less the same thing as your current constructor:
 
ptr->Set(50,100,10,20);


Of course, in my default constructor I set everything to zero as an example. You may be able to default the values to something more meaningful.

Hope this helps.
Last edited on
1
2
3
4
5
6
7
CButton *pBtn = (CButton*)operator new(sizeof(CButton)*4); //edit: sorry not so elegant anymore
for(unsigned i = 0; i < 4; ++i)
{
    new (pBtn+i) CButton(arg1, arg2, arg3);
}
//...
delete pBtn;
Last edited on
That looks nice and elegant, L B. How does it work?

I've never used the operator keyword in that context. Would you care to explain that one for me?
Oops, bit rusty. Here's how it works:
http://ideone.com/bymmL
On this site: http://www.cplusplus.com/reference/std/new/operator%20new/
Last edited on
I see. That makes more sense. :-)
And oops, no one noticed the memory leak! I don't know how to fix it. All I know is how to loop through and delete the CButtons, I don't know how to delete the pBtn's operator-new-ed memory.
all right,

Thanks for all reply !

I think my problem is solved, so I shall mark it as solved

I would more prefer the default constructor way for its simplicity. And I will still consider to learn the other two useful alternative for my coding too

As for the memory leak, you can still discuss it anyway :-)
Isn't my code the same as yours, L B? I have shown how to fix the memory leak... And don't say I copied you, I posted it before you :33 (J/K)
I guess now that I think about it it is the same, the only difference being that you used a pointer-to-pointer-to-CButton and I used a pointer-to-CButton. Yours is somewhat easier to understand. X)
Topic archived. No new replies allowed.