Summary of knowledge of C++ dynamic object array

Default constructor

    First, let's take a look at what is the default constructor, such as the following piece of code:

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
    double x, y;
public:
    void print();
    Point(double a=0,double b=1){x=a;y=b;}  //默认构造函数
    Point(double a){x=1;y=1;} //构造函数
    Point(){} //默认构造函数

};
void Point::print()
{
    cout << x << " " << y << endl;
}

    Therefore, you can know that the default constructor can generally be used when the object is generated without passing in parameters. The corresponding situation is:

Point point;   
Point *point=new Point;

    Non-default constructors must pass parameters. There are the following knowledge points about the default constructor:

  • The program does not define any constructor, there is a default constructor Point(){}, and the values ​​of the members are not initialized
  • There are two ways to define a default constructor, as shown in the above code, one is to define a constructor without parameters, and the other is to define a constructor with default values ​​for all parameters
  • Note: A class can only have one default constructor! In other words, the above two methods can not appear at the same time, generally choose Point (); this form of default constructor

Dynamic array with default constructor

    Let's take a look if a class has a default constructor, the following code is to generate static and dynamic arrays:

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
    double x, y;
public:
    void print();

};
void Point::print()
{
    cout << x << " " << y << endl;
}
int main() {
    //静态对象数组
    Point pp[5];      
    for (int i = 0; i<5; i++)
        pp[i].print();
    //动态对象数组
    Point *p = new Point[5]; //这里面5也可以是变量
    for (int i = 0; i<5; i++)
            p[i].print();
    //别忘记delete相应的空间
}

    It can be seen that if a class has a default constructor, then there is no need to pass in parameters when generating it, which is a lot more convenient.

Dynamic array without default constructor

    If a class does not have a default constructor, in this case you need to pass in parameters, as in the following code:

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
    double x, y;
public:
    Point(double a, double b);
    void print();
};
Point::Point(double a, double b)
{
    x = a;
    y = b;
}
void Point::print()
{
    cout << x << " " << y << endl;
}
int main()
{
    Point *p[5];//静态指针数组
    for (int i = 0; i < 5; i++) {
        p[i] = new Point(i, i);    //其中你可以用自己的方式来初始化对象,例如从键盘接收值等
    }
    for (int i = 0; i < 5; i++) {
        p[i]->print();
    }
    Point **pp = new Point *[5];  //动态指针数组,5也可以是变量
    for (int i = 0; i < 5; i++) {
        pp[i] = new Point(i, i);
    }
    for (int i = 0; i < 5; i++) {
        pp[i]->print();
    }
    //别忘了delete相应的空间
    return 0;
}

    The static pointer array is defined some Pointpointers, through the new way to make each pointer point to the object. The dynamic pointer array allocates a continuous space of a fixed size, each element type in it is Point *, and then the first address of this continuous space is given to pp, which is a pointer to Point * type.

Note that it is new Point *[5] instead of new (Point *)[5], the latter compiler will report an error

new multidimensional array

int *p=new int[2];
int *p=new int[2][3];  //错误
int (*p)[3]=new int[2][3];  //正确
int (*p)[4][5]=new int[2][4][5]; //正确

    Why is the second line wrong? In fact, we can understand it like this:

  • int (*p)[3] where p is a pointer. This is beyond doubt. What kind of data does this pointer point to? Let's change the way of writing, programming int[3] (*p), is it very concise and clear? The type pointed to by p is int[3].
  • The first address of the two int[3] type arrays returned by new int[2][3] is assigned to p.
  • In the same way, the type pointed to by p in the fourth line is int[4][5], and new int[3][4][5] returns the first address of 3 arrays of type int[4][5], assignment Give p.
  • For the first line, if the initial address of p is 1000, assuming that int occupies 4 bytes, then after executing p++, the address of p is 1000+4, and in the third line, if the initial address of p is 1000, after performing p++, the value of p is 1000+4*3, and so on, the value of p after p++ in the fourth line is 1000+4*4*5.

Guess you like

Origin blog.csdn.net/u012397189/article/details/78784928