C++ 动态对象数组的知识总结

https://blog.csdn.net/u012397189/article/details/78784928

默认构造函数

首先,我们来看一下是什么是默认构造函数,如下面一段代码:

#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;
}

 
所以,可以知道默认构造函数总得来说是可以在对象生成的时候,不需要传入参数,对应情况就是:

Point point;   
Point *point=new Point;

 
非默认构造函数就是必须要传参。关于默认构造函数有以下知识点:

  • 程序没有定义任何构造函数,则有默认的构造函数Point(){},里面的成员的值没有初始化
  • 定义默认构造函数有两种方式,如上述代码展示的,一是定义一个无参的构造函数,二是定义所有参数都有默认值的构造函数
  • 注意:一个类只能有一个默认构造函数!也就是说上述两种方式不能同时出现,一般选择 Point(); 这种形式的默认构造函数
     

带默认构造函数的动态数组

我们来看一下如果一个类带默认构造函数,如下代码是生成静态和动态数组的:

#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相应的空间
}

 
可以看出来,如果一个类带默认构造函数的话,那么生成的时候就不需要传入参数了,方便了不少。
 

没有默认构造函数的动态数组

如果一个类没有默认构造函数呢,这种情况就需要传入参数了,如下代码:

#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;
}

 
静态指针数组是定义了一些的Point指针,通过new的方式来使每个指针指向对象。动态指针数组是分配固定大小的连续空间,里面的每个元素类型是Point *,然后把这连续空间的首地址给pp,pp是指向Point *类型的指针。
 

注意是 new Point *[5]而不是 new (Point *)[5],后者编译器会报错
 

new多维数组

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]; //正确

 
    为啥第二行就错误呢?其实我们可以这样理解:

- int (*p)[3]其中p是指针,这是毋庸置疑的,那这个指针指向什么类的数据呢?
    我们改一下写法,编程int[3] (*p),是不是就很简洁明了呢?p指向的类型是int[3]。
- new int[2][3]返回的2个int[3]类型的数组的首地址,赋值给p。
- 同理,第四行p指向的类型是int[4][5],new int[3][4][5]返回的是3个int[4][5]类型的数组的首地址,赋值给p。
- 针对第一行的情况,如果p的一开始地址为1000,假设int占用4个字节,那么执行p++以后,p的地址为1000+4,
    第三行的情况,如果p的一开始地址为1000,进行p++以后,p的请值为1000+4*3,以此类推,第四行p++以后p的值为1000+4*4*5。












种一棵树最好的时间是十年前,其次是现在。
原文地址:https://www.cnblogs.com/islch/p/12691223.html