c++ - C++中如何在函数内调用函数?

标签 c++ singleton

问题:通过 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/

相关文章:

c++ - 你能给我一些 C++ 中奇怪的单行注释的例子吗?

c++ - 包含和使用声明

swift - 如何在 Swift 中创建与泛型共存的单一类

java - Android中的单例类如何使用?

c++ - 使用 MINGW 在窗口应用程序中抑制系统 ("command") 控制台窗口

c++ - 什么是 'linkonce' 部分

c++ - 使用 std::vector 作为 std::map 的键在未找到和使用 reserve() 时不返回 end()

python - 为什么 Flask 中的应用程序上下文不是应用程序的单例?

java - javax.inject.Singleton 和 javax.ejb.Singleton 的区别

java - 同时多次调用方法的问题