且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在分配数组时调用构造函数

更新时间:2023-11-13 14:02:34

对不起我的代码错了..这是正确的吗?


< code>


int N = 22;

pointerArray * MyClass;

pointerArray = new MyClass [N];

for(int i = 0; i< N; i ++)

pointerArray [i] - > MyClass(i);


< / code>


Philipp写道:
抱歉我的代码错了......这是正确的吗?

< code>

int N = 22;
pointerArray * MyClass;


这不是指针数组。它是一个指向数组的指针。

pointerArray = new MyClass [N];


new创建一组N个MyClass类型的对象。

for(int i = 0; i< N; i ++)
pointerArray [i] - > MyClass(i);




这不好。 MyClass类型已经构建。而你

不能调用构造函数,他们没有名字。你想做什么?


-

Attila aka WW


Philipp写道:
int N = 22;
pointerArray * MyClass;


你的意思是''MyClass * pointerArray;''。为什么不发布实际代码?

pointerArray = new MyClass [N];


这次调用MyClass的默认构造函数N次。

for(int i = 0; i< N; i ++)
pointerArray [i ] - > MyClass(i);




应该是

for(int i = 0; i< N; ++ i )pointerArray [i] = MyClass(i);



for(int i = 0; i< N; ++ i)

{

pointerArray [i] .~MyClass();

new(& pointerArray [i])MyClass(i);

}


如果你分配原始内存而不是对象:

pointerArray = reinterpret_cast< MyClass *>

(new char [N * sizeof(MyClass)]);


然后你不需要在for循环中

placement new之前调用析构函数。但是,在那个

的情况下你需要:


for(int i = 0; i< N; ++ i)pointerArray [i]。 ~MyClass();

delete [] reinterpret_cast< char *> (pointerArray);


而不仅仅是''delete [] pointerArray;''。


基本上,重点是你可以'在

对象上调用构造函数(因为当它是一个对象时,它已经构造了

)。


我希望这会有所帮助,并且我没有犯过太多错误。

问候,

巴斯特。
>


Hello, a very simple question:
Ok I have a class MyClass with a constructor MyClass(int) (no constructor
without argument defined)

how can I make an array of pointers to objects of that class, calling the
constructor with the index number as argument?

<code>
int N = 22;
pointerArray = new MyClass*[N];
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);
</code>

is this correct? does the second line call some default constructor for
MyClass? Any better idea how to do that?

Thanks Phil

Sorry my code was wrong... Is this correct?

<code>

int N = 22;
pointerArray* MyClass;
pointerArray = new MyClass[N];
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);

</code>


Philipp wrote:
Sorry my code was wrong... Is this correct?

<code>

int N = 22;
pointerArray* MyClass;
This is not a pointer array. It is a pointer to an array.
pointerArray = new MyClass[N];
new creates an array of N pieces of objects of MyClass type.
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);



This is not good. The MyClass types are already constructed. And you
cannot call constructors, they have no name. What do you want to do?

--
Attila aka WW


Philipp wrote:
int N = 22;
pointerArray* MyClass;
You mean ''MyClass * pointerArray;''. Why not post the real code?
pointerArray = new MyClass[N];
This calls the default constructor for MyClass N times.
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);



Should be
for (int i = 0; i < N; ++ i) pointerArray [i] = MyClass (i);
or
for (int i = 0; i < N; ++ i)
{
pointerArray [i].~MyClass ();
new (& pointerArray [i]) MyClass (i);
}

If you allocate raw memory instead of objects:
pointerArray = reinterpret_cast <MyClass *>
(new char [N * sizeof (MyClass)]);

then you don''t need the destructor call before the
placement new inside the for loop. However, in that
case you would need:

for (int i = 0; i < N; ++ i) pointerArray [i].~MyClass ();
delete [] reinterpret_cast <char *> (pointerArray);

instead of just ''delete [] pointerArray;''.

Basically, the point is that you can''t call a constructor on an
object (because by the time it is an object, it has already been
constructed).

I hope this helps, and that I haven''t made too many mistakes of my own.
Regards,
Buster.