在结构中定义没有默认构造函数的 class
Defining a class with no default constructor inside a struct
我遇到了一个问题,我必须在嵌入式系统中使用 C++ 库。该库(用于微控制器的 Tensorflow Lite)是为嵌入式系统设计的,可以在没有 malloc/free 的情况下使用,但是使用它的示例定义了堆栈上的所有内容,并且没有可用的默认构造函数。
在堆栈上定义在我的应用程序中不起作用,因为我需要能够在函数中初始化它然后退出。我需要的是能够分配一个内存区域(使用我自己的分配器),其中包含我需要的所有内容,然后初始化该内存区域中的所有内容。
我试图将其归结为一个最低限度的例子:
// This is a third party library
class OtherClass {};
class MyClass {
private:
OtherClass &c;
public:
MyClass(OtherClass &c) : c(c) {};
};
// My code
typedef struct {
OtherClass otherClass;
MyClass myClass;
} MyAllocatedData;
char dataIsSomewhere[sizeof(MyAllocatedData)];
MyAllocatedData *pData = (MyAllocatedData *)dataIsSomewhere;
int main() {
// dataIsSomewhere gets allocated somewhere on demand
// now we want to initialise everything
pData->myClass = MyClass(pData->otherClass);
return 0;
}
编译失败:
test.cpp: In function ‘int main()’:
test.cpp:20:45: error: use of deleted function ‘MyClass& MyClass::operator=(MyClass&&)’
pData->myClass = MyClass(pData->otherClass);
^
test.cpp:3:7: note: ‘MyClass& MyClass::operator=(MyClass&&)’ is implicitly deleted because the default definition would be ill-formed:
class MyClass {
^~~~~~~
test.cpp:3:7: error: non-static reference member ‘OtherClass& MyClass::c’, can’t use default assignment operator
有解决这个问题的好方法吗?修补库是可能的,但我宁愿不这样做。
也许我可以为 MyClass
定义自己的 new
运算符,将其分配到正确的位置?不过好像不太理想。
您可以使用 placement new 在给定位置创建对象:
new (&pData->myClass) MyClass(pData->otherClass);
使用正确的构造函数:
struct MyAllocatedData
{
MyAllocatedData() : otherClass(), myClass(otherClass) {}
OtherClass otherClass;
MyClass myClass;
};
然后
int main() {
MyAllocatedData data;
// ...
}
或者如果你需要将它放在内存中,使用新的放置:
MyAllocatedData* pData = new (dataIsSomewhere) MyAllocatedData{};
我遇到了一个问题,我必须在嵌入式系统中使用 C++ 库。该库(用于微控制器的 Tensorflow Lite)是为嵌入式系统设计的,可以在没有 malloc/free 的情况下使用,但是使用它的示例定义了堆栈上的所有内容,并且没有可用的默认构造函数。
在堆栈上定义在我的应用程序中不起作用,因为我需要能够在函数中初始化它然后退出。我需要的是能够分配一个内存区域(使用我自己的分配器),其中包含我需要的所有内容,然后初始化该内存区域中的所有内容。
我试图将其归结为一个最低限度的例子:
// This is a third party library
class OtherClass {};
class MyClass {
private:
OtherClass &c;
public:
MyClass(OtherClass &c) : c(c) {};
};
// My code
typedef struct {
OtherClass otherClass;
MyClass myClass;
} MyAllocatedData;
char dataIsSomewhere[sizeof(MyAllocatedData)];
MyAllocatedData *pData = (MyAllocatedData *)dataIsSomewhere;
int main() {
// dataIsSomewhere gets allocated somewhere on demand
// now we want to initialise everything
pData->myClass = MyClass(pData->otherClass);
return 0;
}
编译失败:
test.cpp: In function ‘int main()’:
test.cpp:20:45: error: use of deleted function ‘MyClass& MyClass::operator=(MyClass&&)’
pData->myClass = MyClass(pData->otherClass);
^
test.cpp:3:7: note: ‘MyClass& MyClass::operator=(MyClass&&)’ is implicitly deleted because the default definition would be ill-formed:
class MyClass {
^~~~~~~
test.cpp:3:7: error: non-static reference member ‘OtherClass& MyClass::c’, can’t use default assignment operator
有解决这个问题的好方法吗?修补库是可能的,但我宁愿不这样做。
也许我可以为 MyClass
定义自己的 new
运算符,将其分配到正确的位置?不过好像不太理想。
您可以使用 placement new 在给定位置创建对象:
new (&pData->myClass) MyClass(pData->otherClass);
使用正确的构造函数:
struct MyAllocatedData
{
MyAllocatedData() : otherClass(), myClass(otherClass) {}
OtherClass otherClass;
MyClass myClass;
};
然后
int main() {
MyAllocatedData data;
// ...
}
或者如果你需要将它放在内存中,使用新的放置:
MyAllocatedData* pData = new (dataIsSomewhere) MyAllocatedData{};