在C++语言中,如何创建一个指向数组的指针的新对象?

In C++ language, how do you create a new object which is a pointer to an array?

本文关键字:数组 指针 对象 新对象 一个 语言 C++ 何创建 创建      更新时间:2023-10-16

我正在使用一个第三方库,其中的方法以这种方式声明:

void nexLoop(NexTouch *nex_listen_list[]);

如果是单个.cpp文件,这是有效的:

NexButton b0 = NexButton(0, 1, "b0");
NexTouch *nex_listen_list[] = 
{
&b0,
NULL
};
...
nexLoop(nex_listen_list);

完整的示例代码可以在这里看到:https://github.com/itead/ITEADLIB_Arduino_Nextion/blob/master/examples/CompText/CompText.ino

现在,我将这个库合并到一个具有单独头文件和 c 文件的类中,以便:

MyNextion.h

#include "Nextion.h"
class MyNextion {
public:
MyNextion();
void loop();
private: 
NexButton *b0;
NexTouch *nex_listen_list[];
}

我的下一个.cpp

#include "MyNextion.h"
MyNextion::MyNextion() {
b0 = new NexButton(0, 1, "b0");
nex_listen_list = new ???
}
MyNextion::loop() {
nexLoop(nex_listen_list); // ???
}

我弄乱了指针和地址运算符,但我找不到成功编译的正确组合。

我的问题是:你会在这里放什么:nex_listen_list = new ???;

另外,我不确定标题是否很好地定义了我的问题。如果您对标题有更好的想法,请更正它。

谢谢!

不应声明没有大小的成员数组。相反,您可以声明一个指针到指针:

class MyNextion {
...
private: 
NexButton *b0;
NexTouch **nex_listen_list;
};

然后施工很容易:

nex_listen_list = new NexTouch*[10];
nex_listen_list[0] = b0;
....

也就是说,nex_listen_list是指向NexTouch对象的指针的动态数组。完成后不要忘记delete[]

delete[] nex_listen_list;

也就是说,如果数组的大小是恒定的,那么普通数组就可以了:

NexButton *b0;
NexTouch *nex_listen_list[3];

如果没有,我宁愿使用std::vector.像这样:

class MyNextion {
...
private: 
NexButton *b0;
std::vector<NexTouch*> nex_listen_list;
};

然后不需要newdelete

nex_listen_list.push_back(b0);
nexLoop(nex_listen_list.data());

继续,您可以将b0声明为完整对象,而不是指针:

class MyNextion {
...
private: 
NexButton b0;
std::vector<NexTouch*> nex_listen_list;
};
...
nex_listen_list.push_back(&b0); //use the pointer!

没有new所以没有delete,也没有泄漏。

如果nex_listen_list具有静态大小,则应按值保留它:

// Interface
class MyNextion {
public:
MyNextion() {}
void loop();
private:
NexButton b0{0, 1, "b0"};
NexTouch *nex_listen_list[2] = {&b0, nullptr};
};
// Implementation
void MyNextion::loop() {
nexLoop(nex_listen_list);
}

如果大小可变,则有两种选择。如果您不介意动态内存分配,请使用std::vector

// Interface
class MyNextion {
public:
MyNextion() {}
void loop();
void foo();
private:
NexButton b0{0, 1, "b0"};
NexButton b1{0, 2, "b1"};
std::vector<NexTouch *> nex_listen_list{&b0, nullptr};
void append(NexTouch *t);
};
// Implementation
void MyNextion::append(NexTouch *t) {
nex_listen_list.back() = t;
nex_listen_list.push_back(nullptr);
}
void MyNextion::foo() {
append(&b1);
}
void MyNextion::loop() {
nexLoop(nex_listen_list.data());
}

或者,您可以预先分配固定大小的列表并避免动态内存分配:

lass MyNextion {
public:
MyNextion() {}
void loop();
void foo();
private:
void append(NexTouch *);
NexButton b0{0, 1, "b0"};
NexButton b1{0, 2, "b1"};
std::array<NexTouch *, 10> nex_listen_list{{&b0, nullptr}};
std::array<NexTouch *, 10>::iterator it_el =
std::find(nex_listen_list.begin(), nex_listen_list.end(), nullptr);
};
// Implementation
void MyNextion::append(NexTouch *t) {
*it_el++ = t;
*it_el++ = nullptr;
}
void MyNextion::foo() {
append(&b1);
}
void MyNextion::loop() {
nexLoop(nex_listen_list.data());
}

你得到了一个相当强大的C++编译器。您应该利用它来为您编写代码。特别是,您应该让编译器为构造函数生成代码。