我想尝试一下Haskell,我希望能够编写一个小型的2D街机游戏(俄罗斯方块或突破).你能推荐一个简单的图形库来帮助我快速入门吗?
顺便说一下,我一直在试验SDL和wxWidgets,但由于依赖性问题,还没有成功运行任何样本,正在努力...
向类添加功能可以通过添加方法或定义将对象作为其第一个参数的函数来完成.我认识的大多数程序员会选择添加实例方法的解决方案.
但是,我有时更喜欢创建一个单独的功能.例如,在下面的示例代码中Area,Diagonal定义为自由函数而不是方法.我发现这种方式更好,因为我认为这些功能提供增强而不是核心功能.
这被认为是好/坏的做法吗?如果答案是"它取决于",那么在添加方法或定义单独函数之间决定的规则是什么?
class Rect
{
public:
Rect(int x, int y, int w, int h) :
mX(x), mY(y), mWidth(w), mHeight(h)
{
}
int x() const { return mX; }
int y() const { return mY; }
int width() const { return mWidth; }
int height() const { return mHeight; }
private:
int mX, mY, mWidth, mHeight;
};
int Area(const Rect & inRect)
{
return inRect.width() * inRect.height();
}
float Diagonal(const Rect & inRect)
{
return std::sqrt(std::pow(static_cast<float>(inRect.width()), …Run Code Online (Sandbox Code Playgroud) 我最近开始欣赏std::auto_ptr,现在我读到它将被弃用.我开始在两种情况下使用它:
例子:
// Exception safe and makes it clear that the caller has ownership.
std::auto_ptr<Component> ComponentFactory::Create() { ... }
// The receiving method/function takes ownership of the pointer. Zero ambiguity.
void setValue(std::auto_ptr<Value> inValue);
Run Code Online (Sandbox Code Playgroud)
尽管复制语义有问题,我觉得auto_ptr很有用.并且似乎没有上述示例的替代方案.
我应该继续使用它,然后切换到std::unique_ptr?还是要避免?
我花了一整天的时间试图找到一种方法来从Qt Creator或Eclipse 启用GDB调试.我了解到基本上有两种方法可以启动目标应用程序:
ssh host gdb)我能够使用这两种方法远程启动gdb并启动应用程序.但是,GDB永远不会响应IDE中设置的任何断点.此外,我无法暂停应用程序以检查程序状态.在Qt Creator中,我只得到一个模糊的堆栈跟踪(我可能一直在查看ssh或gdb的痕迹......).
任何人都可以帮助我开始吗?
我发现使用Qt Creator 2.0有一个名为"附加和调试远程应用程序"的功能.它基于gdbserver.好处是它在IDE的断点处停止.但是,有两个问题:
我应该提一下,远程可执行文件是使用旧版本的GCC编译的,而不是安装在我本地PC上的版本.也许有些问题与此有关.
我应该提一下,我切换到通过SSH在远程机器上运行cgdb.
远程Qt Creator解决方案不稳定.由于神秘的"收到信号"消息,GDB倾向于退出.
使用下面的程序我尝试测试我可以用多快的速度写入磁盘std::ofstream.
在写入1 GiB文件时,我达到大约300 MiB/s.
但是,使用该cp命令的简单文件复制速度至少快两倍.
我的程序是否达到了硬件限制,还是可以更快?
#include <chrono>
#include <iostream>
#include <fstream>
char payload[1000 * 1000]; // 1 MB
void test(int MB)
{
// Configure buffer
char buffer[32 * 1000];
std::ofstream of("test.file");
of.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
auto start_time = std::chrono::steady_clock::now();
// Write a total of 1 GB
for (auto i = 0; i != MB; ++i)
{
of.write(payload, sizeof(payload));
}
double elapsed_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start_time).count();
double megabytes_per_ns = 1e3 / elapsed_ns;
double megabytes_per_s = 1e9 * megabytes_per_ns; …Run Code Online (Sandbox Code Playgroud) 我正在创建一个依赖于其他几个库的插件应用程序(dylib).这些其他库安装在我的系统上,但不保证安装在任何用户的系统上.所以我需要找到一种方法将依赖项与我的应用程序捆绑在一起.
我发现我可以otool用来列出或更改其他dylib的路径.这将允许创建一个文件夹,捆绑我的插件应用程序和所有需要的依赖项.
但是,手动执行此操作似乎是一项耗时且愚蠢的任务.是否有可用于实现自动化的实用程序?
或许我做错了,这个问题有更好更明显的方法吗?
编辑 我创建了一个自动完成大部分任务的脚本.
创建嵌套的dosync调用时会发生什么?子事务是否会在父范围内完成?如果父事务失败,这些子事务是否可逆?
我定义了一个unless宏如下:
user=> (defmacro unless [expr body] (list 'if expr nil body))
#'user/unless
user=> (unless (= 1 2) (println "Yo"))
Yo
Run Code Online (Sandbox Code Playgroud)
你可以看到它工作正常.
现在,在Clojure中,可以通过两种方式定义列表:
; create a list
(list 1 2 3)
; shorter notation
'(1 2 3)
Run Code Online (Sandbox Code Playgroud)
这意味着unless可以在没有list关键字的情况下编写宏.但是,这会导致抛出Java异常:
user=> (unless (= 1 2) (println "Yo"))
java.lang.Exception: Unable to resolve symbol: expr in this context
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会失败吗?
这是我第一次使用C++ 0x rvalue引用进行实验,似乎有些奇怪的事情正在进行中.
在下面的代码示例中,factory函数MakeWindow按值返回Window对象.调用者使用它来初始化Window对象.如果我理解正确,这应该调用移动构造函数.为了检测到这一点,我在那里抛出异常.最重要的是我禁用了复制构造函数:
#include <iostream>
// Fake WinAPI
typedef void* HWND;
HWND CreateWindow() { return (void*)1; }
void DestroyWindow(HWND) { }
// End WinAPI
// C++ WinAPI Wrapper Library
class Window
{
public:
Window(HWND inHandle) :
mHandle(inHandle)
{
std::cout << "Window constructor. Handle: " << inHandle << std::endl;
}
Window(Window && rhs) :
mHandle(rhs.mHandle)
{
std::cout << "Window move constructor. Handle: " << mHandle << std::endl;
rhs.mHandle = 0;
throw 1; // this is my "breakpoint"
} …Run Code Online (Sandbox Code Playgroud) 我有这个示例程序:
#include <iostream>
template<typename Message, typename Decoration, typename PrintImpl>
void print_surrounded(Message&& msg, const Decoration& decoration, const PrintImpl& print_impl)
{
print_impl(decoration); // should forward be used?
print_impl(" ");
print_impl(std::forward<Message>(msg));
print_impl(" ");
print_impl(decoration);
}
template<typename Message, typename PrintImpl>
void pretty_print(Message&& msg, const PrintImpl& print_impl)
{
print_surrounded(std::forward<Message>(msg), "***", print_impl);
}
int main()
{
pretty_print("So pretty!", [](const char* msg) {
std::cout << msg;
});
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用不同的方式传递参数:
&&吗?如果是,我还应该使用std::forward吗?)我做出了正确的选择吗?