简体   繁体   English

C ++数组初始化

[英]C++ Array initialization

the code below gives compilation error when I try to create test t[2]; 当我尝试创建测试t [2]时,以下代码给出了编译错误; because there is no default constructor for this. 因为没有默认的构造函数。

But if I create Test t[2] = {test(1,2), test(2,3)}; 但是如果我创建Test t[2] = {test(1,2), test(2,3)}; Then it works fine. 然后工作正常。

1)But think of a situation, if we want to create more then 100 array element. 1)但是考虑一下情况,如果我们要创建更多的数组元素,则需要100个。 We need to create 100 element in the curly braces like.. Test t[100] = {test(1,2), test(1,2)……/ 100 times /}; 我们需要在花括号中创建100个元素,例如:。t [100] = {test(1,2),test(1,2)……/ 100次 /};

The above code is difficult to maintain. 上面的代码很难维护。 One more solution is to create public member function which takes 2 integers and run in a loop. 另一种解决方案是创建使用2个整数并循环运行的public成员函数。 This solves the problem but i want to know any other good method. 这解决了问题,但是我想知道任何其他好的方法。

2) If I create it using new 2)如果我使用new创建它

Test *t = new test[10];

I get compilation error(No default constructor). 我收到编译错误(没有默认构造函数)。 How to solve this. 如何解决这个问题。

class test
{
    int _a;int _b;

public:
    test(int a, int b);
    void display();
};


int _tmain(int argc, _TCHAR* argv[])
{
    test t[10];

    for (int i = 0 ; i< 10; i++)
        t[i].display();
}

In order to construct your 10 elements in the array the compiler somehow has to instaciate them through a constructor. 为了在数组中构造10个元素,编译器必须以某种方式使它们通过构造器实例化。 For arrays only a default constructor (taking no arguments) can bes used, as you can not pass any arguments to the elements in the array. 对于数组,只能使用默认构造函数(不带参数),因为您不能将任何参数传递给数组中的元素。 Therfor you have to proved a constructor 因此,您必须证明构造函数

test::test()

taking no arguments. 不吵架。

In your example what do you expect to be displayed? 在您的示例中,您希望显示什么?
If you know that, you can write a Default CTor (one that has no parameters) and set your values to the defaults. 如果知道这一点,您可以编写一个默认CTor(一个没有参数的),并将您的值设置为默认值。

An example of the Default CTor: 默认CTor的示例:

// Variant 1: Use the initialization list
test()
: a(-1)
, b(-1)
{
}

// OR
// Variant 2: Do it in the CTor's body
test()
{
    a = -1;
    b = -1;
}

Note: You can write several CTors (it's called "overloading"). 注意:您可以编写多个CTor(称为“过载”)。 One that takes no parameters and sets default values and others that take parameters and set those values. 一个不带参数并设置默认值的参数,另一个不带参数并设置这些值的参数。

You can also define a constructor with default values for all parameters which will be used as the default constructor. 您还可以为所有参数定义默认值的构造函数,该参数将用作默认构造函数。

test(int a = 0, int b = 0) :_a(a), _b(b) {}

Since all parameters have default values, this constructor will be used as the default. 由于所有参数都有默认值,因此将使用此构造函数作为默认值。 Leaving out the initialization list or not initializing the member variables in the body of the constructor may give you random data values. 省略初始化列表或不初始化构造函数主体中的成员变量可能会给您随机数据值。 Some systems may zero all memory allocations, but some do not. 某些系统可能会将所有内存分配归零,而有些则不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM