作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
问题:通过 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 中指定一个实例来调用 changeStatus 之类的 Singleton instance2 = Singleton::getInstance(); instance2->changeStatus
最佳答案
在编写的代码中(纠正所有明显的错误之后)Singleton::runThread()
,当在 instance1
上调用时,确实会调用 Singleton: :changeStatus(int)
在同一个 instance1
上。
关于c++ - 如何在 C++ 中调用函数内部的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592904/
我是一名优秀的程序员,十分优秀!