我已经使用了很多Qt,但最近需要调试我一直在创建的线程,然后找到了我想要的更多线程.
所以我的程序只是一个简单的控制台(没有GUI)Qt应用程序(linux).
我创建的主题:
这就是全部.当我做ps -T ...并找到我的应用程序时有7个线程.我有两个使用信号和插槽的QObjects类,所以他们可能需要一个用于消息处理的线程,这需要我4个线程...所以我不知道为什么我的应用程序可能有7个线程.
任何人都可以解释更多关于发生了什么?可以根据需要发布代码.注意我只new QThread在我的代码中使用一次(目前).
当我试图保持一个关于#includes 的干净头文件时,我发现通过让我的所有成员指向需要包含其他头文件的类/对象,我可以做得更好,因为这样我可以使用前向声明而不是#includes。
但我开始怀疑这是否是一个好的经验法则。
因此,例如采用以下类 - 选项 1:
class QAudioDeviceInfo;
class QAudioInput;
class QIODevice;
class QAudioRx
{
public:
QAudioRx();
private:
QAudioDeviceInfo *mp_audioDeviceInfo;
QAudioInput *mp_audioInput;
QIODevice *mp_inputDevice;
};
Run Code Online (Sandbox Code Playgroud)
但是,这可以像这样重写 - 选项 2:
#include "QAudioDeviceInfo.h"
#include "QAudioInput.h"
#include "QIODevice.h"
class QAudioRx
{
public:
QAudioRx();
private:
QAudioDeviceInfo m_audioDeviceInfo;
QAudioInput m_audioInput;
QIODevice m_inputDevice;
};
Run Code Online (Sandbox Code Playgroud)
选项 1,通常是我的偏好,因为它包含一个更好的清洁标题。但是,选项 2 不需要动态分配/取消分配资源。
也许这可以被视为一个基于主观/意见的问题(我知道意见警察会对此无所适从),所以让我们避免“我喜欢”和“我喜欢”并坚持事实/关键点。
所以我的问题是,一般来说,最佳实践是什么(如果有的话),为什么?或者如果没有一种最佳实践(通常是这种情况),什么时候使用每一种最佳实践?
编辑
抱歉,这实际上是用于私有变量 - 而不是公共变量。
我有以下课程:
class Given
{
public string text = "";
public List<StartCondition> start_conditions = new List<StartCondition>();
};
class StartCondition
{
public int index = 0;
public string device = "unknown";
public string state = "disconnected";
public bool isPass = false;
};
Run Code Online (Sandbox Code Playgroud)
我想将它们转换为c#属性(使用get;和set;)
看看这个问题:什么是get-set-syntax-in-c,似乎我可以像这样使一个属性变得简单易行:
class Given
{
public string text { get; set; }
public List<StartCondition> start_conditions { get; set; }
};
class StartCondition
{
public int index { get; set; }
public string device { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 继我之前的问题:c-sharp-convert-existing-class-to-use-properties-correct
我有一个这样的课:
public class TestCaseInfo
{
public string text { get; set; } = "";
public string requirement_ref { get; set; } = "";
public string given { get; set; } = "";
public string when { get; set; } = "";
public string then { get; set; } = "";
public UInt32 timeLimit { get; set; } = 0;
}
Run Code Online (Sandbox Code Playgroud)
我以前填充这样的结构:
if (!getNodeValue(testcase_node.SelectSingleNode("text"), ref testcaseInfo.text))
errStr += nodeError(testcase_node, "Missing 'text' node");
Run Code Online (Sandbox Code Playgroud)
注意:我试图通过引用传递它.我已经阅读了大量的排队,基本上都说你不能这样做.很公平...
所以我想传递"真实"值(我认为它称为支持值?).就像是:
if (!getNodeValue(testcase_node.SelectSingleNode("text"), ref testcaseInfo._text)) // …Run Code Online (Sandbox Code Playgroud) 我已经使用nunit在MSVS2013中成功运行我的C#单元测试,使用nunit GUI和nunit控制台.
对于前两个,我可以进入我的调试输出(Console.Write(...)).在MSVS2013中,可以通过在测试后单击"输出"链接查看,在GUI中,调试显示在"输出"窗口中.
当我使用nunit3-console.exe运行时,我只是得到摘要报告.我试着查看XML输出中的内容(在TestResults.xml中),但它只包含更多相同的摘要报告.
如何让我的调试也出现在屏幕上?
我的命令行如下所示:
nunit3-console.exe "c:\path\to\my\assebly.dll" --trace=Verbose --test=Regression.Tests.HelloWorld
Run Code Online (Sandbox Code Playgroud)
测试HelloWorld中有一行Console.Write("Hello World\r\n");,我想看到它出现在屏幕上(标准输出).
给出以下代码:
#include <functional>
#include <string>
#include <iostream>
class test
{
public:
struct info
{
std::string name {""};
std::function<bool()> func;
};
//info my_info { "test_name", [&]{return member_func();} }; // <------ ERROR HERE
std::pair<std::string, std::function<bool()>> my_info_pair { "test_name", [&]{return member_func();} };
bool member_func()
{
std::cout << "member_func\n";
return true;
};
};
int main() {
std::cout << "start\n";
test t;
std::cout << t.my_info_pair.first << std::endl;
t.my_info_pair.second();
std::cout << "end\n";
}
Run Code Online (Sandbox Code Playgroud)
此代码有效。但是,如果我取消注释行的注释-它试图以info与std :: pair 初始化相同的方式初始化结构,则它将失败。我不知道为什么...
错误得到的是:
prog.cc:15:60: error: could not convert '{"test_name", …Run Code Online (Sandbox Code Playgroud) 鉴于以下代码:
#include <functional>
#include <iostream>
struct worker
{
std::function<bool(std::string)> m_callback;
void do_work(std::function<bool(std::string)> callback)
{
m_callback = std::bind(callback, std::placeholders::_1); // <--- replace this with lambda
std::cout << "worker is working..." << std::endl;
callback("worker is complete");
}
};
// pretty boring class - a cut down of my actual class
struct helper
{
bool work_callback(std::string str)
{
std::cout << "Helper: msg from worker: " << str << std::endl;
return false;
}
};
int main()
{
helper the_helper;
worker the_worker;
std::cout << "Main: …Run Code Online (Sandbox Code Playgroud)