Assume a class X
with a constructor function X(int a, int b)
I create a pointer to X as X *ptr;
to allocate memory dynamically for the class.
Now to create an array of object of class X
ptr = new X[sizeOfArray];
until now everything is fine. But what I want to do is creation of the above array of objects should invoke the constructor function X(int a, int b)
. I tried as follows:
ptr = new X(1,2)[sizeOfArray];
As expected It gave me compile time error
error: expected ';' before '[' token|
How can I create an array of objects to invoke the constructor?
SizeOfArray
is entered by the user at runtime.
EDIT:
What I wanted to achieve in not possible as answered by zenith or will be too complex . So how can I use std::vector
for the same?
This seems like a job for placement new...
Here's a basic example:
Run It Online !
#include <iostream>
#include <cstddef> // size_t
#include <new> // placement new
using std::cout;
using std::endl;
struct X
{
X(int a_, int b_) : a{a_}, b{b_} {}
int a;
int b;
};
int main()
{
const size_t element_size = sizeof(X);
const size_t element_count = 10;
// memory where new objects are going to be placed
char* memory = new char[element_count * element_size];
// next insertion index
size_t insertion_index = 0;
// construct a new X in the address (place + insertion_index)
void* place = memory + insertion_index;
X* x = new(place) X(1, 2);
// advance the insertion index
insertion_index += element_size;
// check out the new object
cout << "x(" << x->a << ", " << x->b << ")" << endl;
// explicit object destruction
x->~X();
// free the memory
delete[] memory;
}
EDIT: If I've understood your edit, you want to do something like this:
Run It Online !
#include <vector>
// init a vector of `element_count x X(1, 2)`
std::vector<X> vec(element_count, X(1, 2));
// you can still get a raw pointer to the array as such
X* ptr1 = &vec[0];
X* ptr2 = vec.data(); // C++11
This is not possible in the current C++ standard, unless:
vector
.See:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With