吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 828|回复: 20
收起左侧

[求助] 求助很基础的C++思路

[复制链接]
朱朱你堕落了 发表于 2023-8-21 15:39
100吾爱币
本帖最后由 朱朱你堕落了 于 2023-8-21 15:41 编辑

[C++] 纯文本查看 复制代码
#include "stdafx.h"

void Test()
{
        static int i = 0;
        if (i == 0)
        {
                printf("%s\n", "this is Test!");
                i++;
        }
}

void TestTest()
{
        static int i = 0;
        if (i == 0)
        {
                printf("%s\n", "this is TestTest!");
                i++;
        }
}


int _tmain(int argc, _TCHAR* argv[])
{
        while(1)
        {
                Test();
                TestTest();
        }
        return 0;
}

Test()和TestTest()会被频繁调用,我这里直接用死循环模拟,问,还有没有更简介的代码,这两个函数每个都只printf输出一次。

最佳答案

查看完整内容

[mw_shl_code=cpp,true]void Test(){ printf("%s\n", "this is Test!"); } void TestTest(){ printf("%s\n", "this is TestTest!"); } int main(){ bool testFlag = false; bool testTestFlag = false; for (int ii = 0; ii < 99999; ii++) { if (!testFlag) { Test(); testFlag = true; } if (!testTestFlag) { TestTest(); testTestFlag = true; } } return 0; }[/mw_shl_ ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

only998 发表于 2023-8-21 15:39
[C++] 纯文本查看 复制代码
void Test(){
	printf("%s\n", "this is Test!");
}

void TestTest(){
	printf("%s\n", "this is TestTest!");
}

int main(){
	bool testFlag = false;
	bool testTestFlag = false;

	for (int ii = 0; ii < 99999; ii++) {
		if (!testFlag) {
			Test();
			testFlag = true;
		}
		if (!testTestFlag) {
			TestTest();
			testTestFlag = true;
		}
	}
	return 0;
}


也许这样看起来更符合逻辑?
eclipsa 发表于 2023-8-21 15:56
看不明白你想干嘛。
如果你只是想让两个函数频繁调用。并且一开始就是打印一次说明。
那我建议你函数里面是空的,或者说放你想测试的东西。
仅一次的调用的打印逻辑和调用你在while这边写就行了。
longs75 发表于 2023-8-21 15:59
yes2 发表于 2023-8-21 16:08
本帖最后由 yes2 于 2023-8-21 16:09 编辑

[C++] 纯文本查看 复制代码
class CLASS_A
{
public:
    CLASS_A(){ printf("%s\n", "this is Test!"); }
};
class CLASS_B
{
public:
    CLASS_B(){ printf("%s\n", "this is TestTest!"); }
};
void Test()
{
    static CLASS_A* a = new CLASS_A(); //存在泄露

}

void TestTest()
{
    static CLASS_B* b = new CLASS_B();//存在泄露
}

大概理解你的意思,但是不明白你的最终需求,你的代码本身也已经很OK了,也许你需要说明一下为什么你对你当前的代码不满意,什么地方还想要改进,这样我们才能针对性回答
kenxy 发表于 2023-8-21 16:16
你是想看看i的变化吗
DEATHTOUCH 发表于 2023-8-21 16:27
你的意思是要写好几个函数,这些函数会多次调用,但是只有第一次调用的时候会输出一些东西是吧

这样的话前面几楼也都给出了方案,一个像4楼的用flag记录,或者类似5楼用工厂,第一次使用的时候创建对象,不过要注意泄露问题。
水木杉 发表于 2023-8-21 16:28
上网查了一下,这个方法好像挺不错的,我没验证过,不知道满不满足你的要求:https://blog.csdn.net/zhaominyong/article/details/118722915
KirchoffNZ 发表于 2023-8-21 16:28
设置个全局变量进行判断就能到达互斥的效果了吧,你是想要互斥效果,只能输出一次?
wangarc 发表于 2023-8-21 16:30
#include "stdafx.h"

void Test()
{
        static int i = 0;
        if (i)
            return;
        printf("%s\n", "this is Test!");
        i++;
}

void TestTest()
{
        static int i = 0;
        if (i)
                return;
        printf("%s\n", "this is TestTest!");
        i++;
}


int _tmain(int argc, _TCHAR* argv[])
{
        while(1)
        {
                Test();
                TestTest();
        }
        return 0;
}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 22:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表