C 在不构造它的情况下创建静态对象(无需分配)
C++ create static object without constructing it (without allocation)
im编写无法使用内存分配的嵌入式代码!同样,静态对象(因此在微控制器执行了所有初始化之前构建的主函数)应在初始化后的主内构建,而不是之前。
。人们建议的唯一解决方案是使用静态对象指针,并在初始化过程中分配(用新的)分配(使用新的)。既然我没有其他选择,没有其他解决方案?
我想做的如下:
class A
{
public:
A(int a, bool b)
: myVal1(a), myVal2(b)
{
}
private:
int myVal1;
bool myVal2;
}
class B
{
public:
B(char x)
: myChar1(x) // <-- NO CONSTRUCTION, NO PARAMETER OF MYOBJECTA
{
}
void init()
{
// now i wanna construct myObjectA
myObjectA(123, false);
}
private:
char myChar1;
A myObjectA; // <-- NO CONSTRUCTION, NO PARAMETER
}
static B myObjectB('F'); // <-- NO CONSTRUCTION, NO PARAMETER OF MYOBJECTA
void global_init()
{
// ... do before construction
// now i wanna construct myObjectA
myObjectB.init();
//... do after construction
}
您可以使用足够大的存储区域来创建A
的实例和新的位置以控制您创建它的时间。
例如:
#include #include
struct A {
A(int a, bool b): myVal1(a), myVal2(b) {}
void foo() {}
private:
int myVal1;
bool myVal2;
};
struct B {
B(char x): myChar1(x) {}
~B() { if(ptr) { ptr->~A(); } }
void init() {
ptr = new (&storage) A{123, false};
}
A * myObjectA() {
return ptr;
}
private:
char myChar1;
std::aligned_storage_t<sizeof(A), alignof(A)> storage;
A *ptr{nullptr};
};
static B myObjectB('F');
void global_init() {
// ... do before construction
// now i wanna construct myObjectA
myObjectB.init();
//... do after construction
}
int main() {
global_init();
myObjectB.myObjectA()->foo();
}
这个不会分配内存(如果我得到正确的意思),而A
的实例实际上是在B::init
中创建的(这似乎是您在您在您的写作中的要求问题)。
如果您可以访问支持C 17的编译器,或者可以使用C Boost库,则std::optional
是另一个答案中建议的有效替代方案。无论如何,您没有指定遵守标准的修订,因此...在任何情况下,这都是一种方法。
您有可用的升级吗?
#include <boost/optional.hpp>
class A
{
public:
A(int a, bool b)
: myVal1(a), myVal2(b)
{
}
private:
int myVal1;
bool myVal2;
};
class B
{
public:
B(char x)
: myChar1(x) // <-- NO CONSTRUCTION, NO PARAMETER OF MYOBJECTA
{
}
A& getA() {
assert(myObjectA);
return myObjectA.get();
}
void init()
{
// now i wanna construct myObjectA
myObjectA.emplace(123, false);
}
private:
char myChar1;
boost::optional<A> myObjectA; // <-- NO CONSTRUCTION, NO PARAMETER
};
static B myObjectB('F'); // <-- NO CONSTRUCTION, NO PARAMETER OF MYOBJECTA
void global_init()
{
// ... do before construction
// now i wanna construct myObjectA
myObjectB.init();
//... do after construction
}
int main()
{
global_init();
}
相关文章:
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 类的全局对象和静态成员
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- 将 RTOS 队列对象封装在仅具有静态分配的 IQueue 自定义接口中
- 我可以读取静态对象中的文件.txt吗?C++
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 链接时,不同静态库中的同一对象文件
- 通过 Gazebo 世界插件将静态对象附加到机器人链接
- 将对象创建为全局/静态对象会崩溃,而本地对象不会崩溃
- 静态分配对象的值初始化
- 内联函数的函数本地静态对象是否在共享对象文件之间共享?
- 将基类分配给派生对象,反之亦然,以C++以及静态和动态对象之间的差异
- 如何正确实例化静态字段 tat 是另一个类对象
- 非静态成员引用必须相对于特定对象
- 如何将对象添加到类中的静态向量
- SFML-对象静态定义
- 派生类没有额外的数据成员;将基对象静态向下转换为派生对象是否安全
- 使用全局对象静态成员时出现seg错误
- 卸载应用程序域不清除C++ COM 对象静态成员
- 为什么我不能将模板化对象静态化为继承树