如何初始化数组? [英] How to initialize array?

查看:82
本文介绍了如何初始化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在C ++中初始化数组时,还要初始化它包含的类b / b,它调用默认构造函数。但是,我想

只初始化数组(即保留空格)并使用我特定的

构造函数来初始化类。不使用malloc怎么做?

像Point * pt = new Point [N];我希望它只保留N个点的

空间,而不是调用默认构造函数。我没有

有一个默认的构造函数。事实上,Point是不可变的,即一旦它创建了b $ b,它的x& y值不应该被改变。所以我想要

来创建它并放在数组中。请注意,数组大小仅限于

运行时。 malloc(N * sizeof(Point))是一种安全的方式吗?

即使我保留N点的空间,如何创建积分?

像pt [0] = Point(3,4)这样的东西会起作用吗?

或者我需要新的位置才能将实际的Point放在适当的

位置.Point类没有子类,也没有。

abir

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

推荐答案



totonnapísal(a):

toton napísal(a):



在C ++中初始化数组时,也初始化类
它包含的
,它调用默认的构造函数。但是,我想

只初始化数组(即保留空格)并使用我特定的

构造函数来初始化类。不使用malloc怎么做?
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?



如何使用std :: vector?


class A

{

};

std :: vector< A * v;

v.reserve(100);

How about using std::vector?

class A
{
};
std::vector<A*v;
v.reserve(100);


>

像Point * pt = new Point [N];我希望它只保留N个点的

空间,而不是调用默认构造函数。我没有

有一个默认的构造函数。事实上,Point是不可变的,即一旦它创建了b $ b,它的x& y值不应该被改变。所以我想要

来创建它并放在数组中。请注意,数组大小仅限于

运行时。 malloc(N * sizeof(Point))是一种安全的方式吗?

即使我保留N点的空间,如何创建积分?

像pt [0] = Point(3,4)之类的东西会起作用吗?
>
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?



v.push_back(new A(3,4));

v.push_back(new A(3,4));


或者我需要新的位置把实际的Point放在适当的

位置.Point类没有子类,也没有。

abir
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


toton写道:
toton wrote:



在C ++中初始化数组时,也是初始化它包含的类b / b,它调用默认构造函数。但是,我想

只初始化数组(即保留空格)并使用我特定的

构造函数来初始化类。如何在不使用malloc的情况下完成?
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?



您对此的需求可能表明存在更深层次的问题。要预先回答技术问题:你需要使用malloc(或其他一些原始内存

分配机制,例如,基于new)和之后的新位置

构造你的对象。您可以将所有这些代码放在标准

调用分配器的位置。

Your need for this may indicate a deeper problem. To answer the technical
question up front: you need to use malloc (or some other raw memory
allocation mechanism, e.g., based on new) and placement new afterwards to
construct your objects. You could place all this code in what the standard
calls an "allocator".


像Point * pt这样的东西=新点[N];我希望它只保留N个点的

空间,而不是调用默认构造函数。我没有

有一个默认的构造函数。事实上,Point是不可变的,即一旦它创建了b $ b,它的x& y值不应该被改变。
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.



这可能是一个严重的设计缺陷。你创造了一个不可分配的课程。这排除了标准容器的许多用途。

That could be a serious design flaw. You made a class that is not
assignable. This rules out the many uses of standard containers.


所以我想要

来创建它并放置在数组中。请注意,数组大小仅限于

运行时。 malloc(N * sizeof(Point))是一种安全的方式吗?

即使我保留N点的空间,如何创建积分?

像pt [0] = Point(3,4)这样的东西会起作用吗?

或者我需要新的位置才能将实际的Point放在适当的

位置.Point类没有子类,也没有。
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.



如果你不打算使用多态点,我看不出你的

点类不应该是什么原因;像int一样做。使其可以复制构造,

可分配,如果你愿意,让所有其他成员函数保持不变(如果你有b / b $ b计划有任何!)。一旦你的类表现得像一个具有良好价值的类

语义,你可以使用所有标准容器,如std :: vector和

std :: map来跟踪你的点。这将立即消除首先使用原始数组的需要,并且还可以使你的程序摆脱很多不必要的低级代码。

Best


Kai-Uwe Bux

If you don''t plan on polymorphic points, I do not see any reason why your
point class shouldn''t "do as int does". Make it copy-constructible,
assignable, if you want, make all other member functions constant (if you
plan on having any!). Once your class behaves like a class with good value
semantics, you can use all the standard containers like std::vector and
std::map to keep track of your points. This will at once eliminate the need
to use raw arrays in the first place and also rid your program of a lot of
unnecessary low-level code.
Best

Kai-Uwe Bux


toton写道:
toton wrote:



在C ++中初始化数组时,还要初始化它包含的类b / b,它调用默认构造函数。但是,我想

只初始化数组(即保留空格)并使用我特定的

构造函数来初始化类。不使用malloc怎么做?


像Point * pt = new Point [N];我希望它只保留N个点的

空间,而不是调用默认构造函数。我没有

有一个默认的构造函数。事实上,Point是不可变的,即一旦它创建了b $ b,它的x& y值不应该被改变。所以我想要

来创建它并放在数组中。请注意,数组大小仅限于

运行时。 malloc(N * sizeof(Point))是一种安全的方式吗?

即使我保留N点的空间,如何创建积分?

像pt [0] = Point(3,4)这样的东西会起作用吗?

或者我需要新的位置才能将实际的Point放在适当的

位置.Point类没有子类,也没有。


abir
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



也许你正试图做这样的事情?


#include< cstddef // for size_t

#include< new>


class Point

{

};


int main()

{

const std :: size_t N = 25;


std :: size_t count = 0;

Point * p = 0;

//分配一些未初始化的内存。

p = static_cast< Point *>(operator new(sizeof(Point)* N));


//构造一些点。 (使用新的位置)

for(count = 0; count< N; ++ count)

new(p + count)Point;


//破坏积分。 (显式调用析构函数)

for(count = 0; count< = N; ++ count)

p [count] .~Point();


//释放内存。

运算符删除(p);

}

-

Alan Johnson

Perhaps you are trying to do something like this?

#include <cstddef// for size_t
#include <new>

class Point
{
} ;

int main()
{
const std::size_t N = 25 ;

std::size_t count = 0 ;
Point * p = 0 ;
// Allocate some uninitialized memory.
p = static_cast<Point *>(operator new(sizeof(Point) * N)) ;

// Construct some Points. (using placement new)
for (count = 0; count < N; ++count)
new (p+count) Point ;

// Destruct the points. (explicitly call destructor)
for (count = 0; count <= N; ++count)
p[count].~Point() ;

// Release the memory.
operator delete(p) ;
}
--
Alan Johnson


这篇关于如何初始化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆