T
toton
Hi,
In C++ when I initialize an array it, also initializes the class that
it contains, which calls the default constructor. However, I want to
initialize the array only (i.e reserve the space) and use my specific
constructor to initialize the class. How to do it without using malloc?
Something like Point* pt = new Point[N]; I want it to reserve the
space for N points only, and not to call default constructor. I dont
have a default constructor. Infact the Point is immutable, i.e once it
is created , its x & y values are not supposed to be changed. So I want
to create it and place in the array. Note the array size to be fixed at
runtime only. Is malloc(N*sizeof(Point)) is a safe way to do it?
Even if I reserve the space for N points, how to create the points?
Something like pt[0] = Point(3,4) will work?
Or I need placement new to put the actual Point in the proper
position.The Point class doesn't have a subclass, and wont have.
abir
In C++ when I initialize an array it, also initializes the class that
it contains, which calls the default constructor. However, I want to
initialize the array only (i.e reserve the space) and use my specific
constructor to initialize the class. How to do it without using malloc?
Something like Point* pt = new Point[N]; I want it to reserve the
space for N points only, and not to call default constructor. I dont
have a default constructor. Infact the Point is immutable, i.e once it
is created , its x & y values are not supposed to be changed. So I want
to create it and place in the array. Note the array size to be fixed at
runtime only. Is malloc(N*sizeof(Point)) is a safe way to do it?
Even if I reserve the space for N points, how to create the points?
Something like pt[0] = Point(3,4) will work?
Or I need placement new to put the actual Point in the proper
position.The Point class doesn't have a subclass, and wont have.
abir