我有一个函数指针定义:
typedef void (*EventFunction)(int nEvent);
Run Code Online (Sandbox Code Playgroud)
有没有办法用特定的C++对象实例处理该函数?
class A
{
private:
EventFunction handler;
public:
void SetEvent(EventFunction func) { handler = func; }
void EventOne() { handler(1); }
};
class B
{
private:
A a;
public:
B() { a.SetEvent(EventFromA); } // What do I do here?
void EventFromA(int nEvent) { // do stuff }
};
Run Code Online (Sandbox Code Playgroud)
编辑: Orion指出了Boost提供的选项,例如:
boost::function<int (int)> f;
X x;
f = std::bind1st(
std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
Run Code Online (Sandbox Code Playgroud)
不幸的是Boost对我来说不是一个选择.是否有某种"currying"函数可以用C++编写,它会将指向成员函数的指针包装到普通函数指针中?
我想写的代码是这样的:
void MethodOnThreadA()
{
for (;;)
{
// Do stuff
if (ErrorConditionMet)
ThrowOnThread(threadB, new MyException(...));
}
}
void MethodOnThreadB()
{
try
{
for (;;)
{
// Do stuff
}
}
catch (MyException ex)
{
// Do the right thing for this exception.
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以让线程B以线程安全的方式定期检查线程A是否设置了一个标志,但这会使代码更复杂.有没有更好的机制可供我使用?
以下是定期检查的更加充实的示例:
Dictionary<Thread, Exception> exceptionDictionary = new Dictionary<Thread, Exception>();
void ThrowOnThread(Thread thread, Exception ex)
{
// the exception passed in is going to be handed off to another thread,
// so it needs to be thread …Run Code Online (Sandbox Code Playgroud) \\F#中的Haskell列表差异运算符是否有等价运算符?
我想使用Parsec makeTokenParser来构建我的解析器,但我想使用我自己的定义whiteSpace.执行以下操作将替换whiteSpace为我的定义,但所有lexeme解析器仍使用旧定义(例如,P.identifier lexer将使用旧的whiteSpace).
...
lexer :: P.TokenParser ()
lexer = l { P.whiteSpace = myWhiteSpace }
where l = P.makeTokenParser myLanguageDef
...
Run Code Online (Sandbox Code Playgroud)
查看代码,makeTokenParser我想我明白为什么它以这种方式工作.我想知道是否有任何变通方法可以避免完全复制代码makeTokenParser?
在MSBuild中使用通用引导程序时,如何确定先决条件的安装顺序?
例如,给定:
<Project ToolsVersion="3.5" xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<ItemGroup>
<BootstrapperFile Include='A' />
<BootstrapperFile Include='B' />
<BootstrapperFile Include='C' />
<BootstrapperFile Include='D' />
</ItemGroup>
<Target Name='MySetup'>
<GenerateBootstrapper
Path='C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper'
ApplicationName='My Program'
ApplicationFile='MyProgram.msi'
BootstrapperItems='@(BootstrapperFile)'
Culture='en'
CopyComponents='true'
ComponentsLocation='HomeSite'
OutputPath='.\' />
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud)
A,B,C和D的安装顺序是什么?我如何控制该订单?
.net ×2
c# ×2
haskell ×2
bootstrapper ×1
c++ ×1
exception ×1
f# ×1
function ×1
installation ×1
operators ×1
parsec ×1
parsing ×1
pointers ×1
propertygrid ×1
whitespace ×1
winforms ×1