为什么这样做?
#include <stdio.h>
class ClassA
{
public:
ClassA(int id) : my_id(id) { };
ClassA * makeNewA(int id)
{
ClassA *a = new ClassA(id);
printf("ClassA made with id %d\n", a->getId());
return a;
};
private:
int getId() {
return my_id;
};
private:
int my_id;
};
int main()
{
ClassA a(1);
ClassA *b = a.makeNewA(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
无论这是否是一个好主意,为什么它有效?public函数ClassA::makeNewA(int)实例化一个新的ClassA,然后getId()使用新对象调用一个私有函数.班级自动成为自己的朋友吗?
谢谢
在对一些代码进行基准测试时,我发现即使是最无害的代码更改,它的执行时间也会有所不同.
我试图将下面的代码归结为最小的测试用例,但它仍然相当冗长(为此我道歉).几乎任何改变都会影响基准测试结果.
#include <string>
#include <vector>
#include <iostream>
#include <random>
#include <chrono>
#include <functional>
constexpr double usec_to_sec = 1000000.0;
// Simple convenience timer
class Timer
{
std::chrono::high_resolution_clock::time_point start_time;
public:
Timer() : start_time(std::chrono::high_resolution_clock::now()) { }
int64_t operator()() const {
return static_cast<int64_t>(
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now()-start_time).count()
);
}
};
// Convenience random number generator
template <typename T>
class RandGen
{
mutable std::default_random_engine generator;
std::uniform_int_distribution<T> distribution;
constexpr unsigned make_seed() const {
return static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count());
}
public:
RandGen(T min, T max) : generator(make_seed()), distribution(min, max) { } …Run Code Online (Sandbox Code Playgroud) 我正在试验 Boost beast::websocket websocket_client_async.cpp示例,结合websocket_server_async.cpp。
正如给定的,客户端示例只是建立一个连接,向服务器发送一个字符串(它只是回显),打印回复,关闭并存在。
我正在尝试修改客户端以使会话保持活动状态,以便我可以重复发送/接收字符串。因此,尽管示例代码的on_handshake函数会立即通过 发送字符串ws_.async_write(...),但我将其分离为自己的write(...)函数。
这是我修改后的session课程:
using tcp = boost::asio::ip::tcp;
namespace websocket = boost::beast::websocket;
void fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<tcp::socket> ws_;
std::atomic<bool> io_in_progress_;
boost::beast::multi_buffer buffer_;
std::string host_;
public:
// Resolver and socket require an io_context …Run Code Online (Sandbox Code Playgroud) 我无法理解Xerces-C++内存管理.
如果我有这个(示例)XML文件"config.xml":
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<port>
<reference>Ref1</reference>
<label>1PPS A</label>
<enabled>true</enabled>
</port>
</settings>
Run Code Online (Sandbox Code Playgroud)
而这段代码:
#include <xercesc/dom/DOM.hpp>
XERCES_CPP_NAMESPACE_USE
DOMElement *nextChildElement(const DOMElement *parent)
{
DOMNode *node = (DOMNode *)parent->getFirstChild();
while (node)
{
if (node->getNodeType() == DOMNode::ELEMENT_NODE)
return (DOMElement *)node;
node = node->getNextSibling();
}
return nullptr;
}
int main(int argc, char **argv)
{
XMLPlatformUtils::Initialize();
XMLCh tempStr[100];
XMLString::transcode("LS", tempStr, 99);
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMLSParser *parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMDocument *doc = impl->createDocument(0, 0, 0);
doc = parser->parseURI("config.xml");
DOMElement *el = doc->getDocumentElement(); …Run Code Online (Sandbox Code Playgroud) 如何使用添加多行消息
git commit -a -m "..."
Run Code Online (Sandbox Code Playgroud)
这个类似问题的答案似乎行得通(通过投票和接受来判断),但似乎有点麻烦1。
git 文档内容如下:
-m <msg>
--message = <msg>
使用给定的<msg>作为提交消息。如果给出了多个-m选项,则它们的值将串联为单独的段落。
因此,建议仅对-m消息的每一行使用一个新行吗?还是“段落”添加了额外的行距?
如果将来的命令版本允许我们仅\n在句子之间添加以表示换行符,那就太好了。
1链接的答案基本上建议使用消息模板文件,并通过以下命令直接git使用该文件:git commit -t <template_file>
我想将一堆布尔标志传递给Windows批处理脚本函数.像这样的东西:
1 SETLOCAL
2 SET _debug=1
3 SET _x64=1
4
5 call :COMPILE %_debug% %_x64%
6 :: call again where %_debug% is now 0
7 :: call again where %_x64% is now 0
8 :: etc...
9
10 :COMPILE
11 :: stuff depending on input args
Run Code Online (Sandbox Code Playgroud)
我使用的变量_debug,并_x64打的电话(线5-7)更具可读性,而不是像这样:
call :COMPILE 0 1
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来传递相当于not variable?喜欢:
call :COMPILE ~%_debug% ~%_64%
Run Code Online (Sandbox Code Playgroud)
要么
call :COMPILE (%_debug% eq 1) (%_64% eq 1)
Run Code Online (Sandbox Code Playgroud)
或者我必须声明不像变量那样:
SET _debug=1
SET _not_debug=0
SET …Run Code Online (Sandbox Code Playgroud) 对于此代码:
int main(int argc, char **argv)
{
auto a = static_cast<uint8_t>(sizeof(uint64_t));
auto b = 8 * static_cast<uint8_t>(sizeof(uint64_t));
auto c = static_cast<uint32_t>(sizeof(uint64_t));
auto d = 8 * static_cast<uint32_t>(sizeof(uint64_t));
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
a解决的类型unsigned char,b解决的类型int,c解决的类型unsigned int和,d解决的类型unsigned int我希望这些结果a,c以及d,但我的困惑b.
64显然适合8位unsigned char.有人可以解释一下吗?
c++ ×5
batch-file ×1
benchmarking ×1
boost ×1
function ×1
git ×1
performance ×1
visual-c++ ×1
websocket ×1
x86 ×1
xerces-c ×1