有没有人知道是否有一个技巧切换所有cout <<功能不打印出可见输出?我试图将我和其他人编写的一些代码组合起来组合一个演示.我宁愿不将输出重定向到一个文件,并希望有一个在Windows和Linux之间具有一定程度兼容性的解决方案.
在我的场景中,我有许多代码行,#defines当某些方法产生调试输出时,它们具有各种控制.我想打电话给:
cout.off();
driverForAffectA();
driverForAffectB();
cout.on();
printSpecializedDebug();
exit(0);
Run Code Online (Sandbox Code Playgroud) 我有三个文件如下
Test.cpp
void helloworld()
{
disable pf;
pf.Disable();
printf("No statement \n");
}
int main()
{
disable dis;
helloworld();
printf("Hello World");
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
disable.cpp
#include "StdAfx.h"
#include "disable.h"
disable::disable(void)
{#define printf(fmt, ...) (0)}
disable::~disable(void)
{}
void disable::Disable()
{
#define printf(fmt, ...) (0)
}
Run Code Online (Sandbox Code Playgroud)
disable.h
#pragma once
class disable
{
public:
disable(void);
~disable(void);
void Disable();
};
Run Code Online (Sandbox Code Playgroud)
执行后,我得到输出为No Statement Hello World.但我想two printf statements通过调用Disable function和禁用它们disable constructor..请帮助我为什么它不工作以及如何解决这个问题.请帮忙.
但是,如果我愿意,事情就可以了
main()
{
#define printf(fmt, ...) (0)
printf("Hello …Run Code Online (Sandbox Code Playgroud)