之前未调用函数时出错
Giving an error when a function is not called before
我正在尝试管理基于 C++ 的应用程序中的初始化函数。我愿意:
确保 init_some_hw_peripherals()
(见下文)即使多次调用 init()
也只运行一次。
如果先前未从对象调用另一个函数 init_some_hw_peripherals()
,则阻止函数 doPerform()
被执行。
我想到了以下解决方案,但它不起作用(如果未初始化,我不会收到任何错误消息)。我知道为什么它不起作用,而且它非常有道理。但我想使用这些类型的定义来实现我提到的目标。希望这些信息对您有所帮助。
如果你能帮助我处理这种情况并给我一些指导,我将不胜感激。
提前致谢。
初始化
void myClass::init(void)
{
#ifndef MY_INIT_
#define MY_INIT_
init_some_hw_peripherals();
#endif
}
申请
void myClass::perform (void)
{
#ifndef MY_INIT_
#error "You havent initialized. Use myClass::init()"
#else
doPerform();
#endif
}
编辑:对我来说使用私有变量的问题是我有几个 类 可以调用 init 函数。所以我不想让它成为一个私有变量。这就是为什么我坚持使用这种解决方案,因为我知道它最初不会起作用。
你可以使用私有成员来告诉你初始化是否完成:
class myClass {
public:
myClass() : isInit(false) {}
...
private:
bool isInit;
};
void myClass::init(void)
{
if (!init) {
init_some_hw_peripherals();
init = true;
}
}
void myClass::perform (void)
{
if (!init) {
cout << "You havent initialized. Use myClass::init()";
} else {
doPerform();
}
}
通常 init
类函数的存在表明缺少合适的构造函数。给定的要求显然导致两个 类 具有专用构造函数:
class hwPeripherals
{
private: static ::std::atomic_bool s_construction_attempted;
// instead of init_some_hw_peripherals
public: hwPeripherals(void)
{
// Check that no attempts of hwPeripherals construction happened yet.
if(s_construction_attempted.exchange(true))
{
// Throw an exception.
}
// Initialization... Throw an exception if fails.
}
};
class myClass
{
private: myClass(void) = delete;
// instead of init
public: explicit myClass(hwPeripherals & peripherals)
{
// Initialization... Throw an exception if fails.
}
public: void perform(void);
};
这种调用perform user的方式需要先实例化myClass
,为了实现这个user需要实例化hwPeripherals
:
hwPeripherals peripherals{};
myClass obj{peripherals};
obj.perform();
我正在尝试管理基于 C++ 的应用程序中的初始化函数。我愿意:
确保
init_some_hw_peripherals()
(见下文)即使多次调用init()
也只运行一次。如果先前未从对象调用另一个函数
init_some_hw_peripherals()
,则阻止函数doPerform()
被执行。
我想到了以下解决方案,但它不起作用(如果未初始化,我不会收到任何错误消息)。我知道为什么它不起作用,而且它非常有道理。但我想使用这些类型的定义来实现我提到的目标。希望这些信息对您有所帮助。
如果你能帮助我处理这种情况并给我一些指导,我将不胜感激。
提前致谢。
初始化
void myClass::init(void)
{
#ifndef MY_INIT_
#define MY_INIT_
init_some_hw_peripherals();
#endif
}
申请
void myClass::perform (void)
{
#ifndef MY_INIT_
#error "You havent initialized. Use myClass::init()"
#else
doPerform();
#endif
}
编辑:对我来说使用私有变量的问题是我有几个 类 可以调用 init 函数。所以我不想让它成为一个私有变量。这就是为什么我坚持使用这种解决方案,因为我知道它最初不会起作用。
你可以使用私有成员来告诉你初始化是否完成:
class myClass {
public:
myClass() : isInit(false) {}
...
private:
bool isInit;
};
void myClass::init(void)
{
if (!init) {
init_some_hw_peripherals();
init = true;
}
}
void myClass::perform (void)
{
if (!init) {
cout << "You havent initialized. Use myClass::init()";
} else {
doPerform();
}
}
通常 init
类函数的存在表明缺少合适的构造函数。给定的要求显然导致两个 类 具有专用构造函数:
class hwPeripherals
{
private: static ::std::atomic_bool s_construction_attempted;
// instead of init_some_hw_peripherals
public: hwPeripherals(void)
{
// Check that no attempts of hwPeripherals construction happened yet.
if(s_construction_attempted.exchange(true))
{
// Throw an exception.
}
// Initialization... Throw an exception if fails.
}
};
class myClass
{
private: myClass(void) = delete;
// instead of init
public: explicit myClass(hwPeripherals & peripherals)
{
// Initialization... Throw an exception if fails.
}
public: void perform(void);
};
这种调用perform user的方式需要先实例化myClass
,为了实现这个user需要实例化hwPeripherals
:
hwPeripherals peripherals{};
myClass obj{peripherals};
obj.perform();