gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:54:28 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com