在C++语言中,如何创建一个指向数组的指针的新对象?
In C++ language, how do you create a new object which is a pointer to an array?
我正在使用一个第三方库,其中的方法以这种方式声明:
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;
};
然后不需要new
或delete
:
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++编译器。您应该利用它来为您编写代码。特别是,您应该让编译器为构造函数生成代码。
相关文章:
- 添加到数组指针
- C++语法差异:二维和一维数组(指针算术)
- 数组指针表示法C++(移动数组时)
- 复制后删除原始数组指针将前 3 个字节设置为 0
- C++访问指向结构的指针中的类数组指针
- C++编译时使用 constexpr 字符数组指针分配静态数组?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- 创建<int>对整数数组指针的矢量引用 (C++)
- 将 2D 数组指针传递给 C++ 中的函数
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- 队列数组指针 (C++)
- C++数组指针上的删除操作
- 对于循环不循环和检测字符数组 [指针和字符数组]
- 如何初始化数组指针对象
- 如何正确传递 2D 数组指针作为参数
- 从数组指针中获取怪异的数字
- 初始化std :: unique_ptr作为原始数组指针的初始化
- 将结构数组指针从C#传递到C
- STD :: Sort将数组指针设置为NULL
- C++数组指针错误无法将“int*”转换为“int**”