问题:通过 C++ 函数,我需要运行线程函数,该函数又调用另一个 Singleton C++ 函数。这个被调用的函数将调用C函数(它运行一个无限循环,每10毫秒改变嵌入式系统状态)。
问题:如何在 C++ 中的函数内部调用函数?我需要分配实例来调用第二个函数吗?
请引用示例代码并给出您的想法是否正确。
我有一个单例类,说 Singleton
class Singleton
{
private : // constructors and values
public :
void runThread();
Singleton getInstance();
bool ChangeStatus( int a);
};
void Singleton:: runThread()
{
changeStatus( 7); // is this is right way to call function inside function
}
bool Singleton:: changeStatus( int a);
{
// This calls C function which changes the status of embedded system
}
void main()
{
// create instance of singleton class
Singleton *instance1 = Singleton::getInstance();
instance1.runThread();
/* will this call the function changeStatus and will this
changeStatus function will change status of embedded system
assuming the c function to change status is working fine.
*/
}
请忽略基本语法错误。
当我从 main 调用 runThread
函数时,它会成功调用 changeStatus 函数吗?或者我是否需要在 runThread 中再指定一个实例来调用类似 Singleton 的changeStatus
instance2 = Singleton::getInstance();实例2->更改状态
最佳答案
在编写的代码中(更正所有明显错误后)Singleton::runThread()
,当在 instance1
上调用时,确实会调用 Singleton: :changeStatus(int)
在同一个 instance1
上。
关于c++ - C++中如何在函数内调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592904/