如何使用任意非静态类方法调用 AfxBeginThread?也许我可以用boost bind做点什么?以下是 Microsoft 的预期用法(并且是调用非静态方法的示例,但它是硬编码的方法):
UINT MyThreadProc( LPVOID pParam )
{
CMyObject* pObject = (CMyObject*)pParam;
if (pObject == NULL ||
!pObject->IsKindOf(RUNTIME_CLASS(CMyObject)))
return 1; // if pObject is not valid
// do something with 'pObject'
return 0; // thread completed successfully
}
// inside a different function in the program
...
pNewObject = new CMyObject;
AfxBeginThread(MyThreadProc, pNewObject);
Run Code Online (Sandbox Code Playgroud) 我有一个全局函数将一个对象(或类型Source)的相关位复制到另一个(类型Target),如下所示:
template<typename Source , typename Target>
void partialCopy( Source& source , Target& target )
{
// perform copy
}
Run Code Online (Sandbox Code Playgroud)
我发现全局函数的问题在于,与成员函数不同,编码两个参数中的哪一个是源,哪个是目标时,不能立即清楚.因此,我想partialCopy()在每个类中都有一个成员函数,如下所示:
struct Foo
{
template<typename T>
void partialCopy( T& target )
{
::partialCopy( *this , target );
}
};
Run Code Online (Sandbox Code Playgroud)
现在的问题是必须将成员函数复制到几十个类.这是一个可以容忍的复制和粘贴编程案例吗?我已经考虑过放入partialCopy一个头文件partialCopy.h并使用预处理器include将它'注入'到每个类中,如下所示:
struct Foo
{
#include "partialCopy.h"
};
Foo f;
Bar b;
f.partialCopy( b );
Run Code Online (Sandbox Code Playgroud)
虽然这是有效的,但我从未在任何地方看到它,也不知道它是否不可接受.
我已经尝试将partialCopy成员函数放在公共基类中并继承它但这不起作用,因为this关键字将引用基类而不是派生类.
还有更好的选择吗?请指教.
编辑
我static_cast在一个CRTP基类中对派生类执行a的John的建议(在一个被删除的线程中)可以很好地工作.@John请发一个答案,我会这样标记.
我在尝试将函数作为参数传递给另一个对象的函数时遇到问题.我很清楚有很多类似的主题,但我要么无法让他们的解决方案工作或无法理解它们.
class foo
{
public:
void func1(void (*Drawing)(void));
template<class T>
void func2(void (T::*Drawing)(void));
};
class bar
{
private:
foo myFoo;
void Drawing();
void func3() {
// Attempt 1
myFoo.func1(Drawing);
// Attempt 2
myFoo.func2<bar>(&bar::Drawing);
}
};
Run Code Online (Sandbox Code Playgroud)
所以在我的第一次尝试中,我得到的错误是你无法转换void (bar::*)(void)为void (*)(void)我发现有正常的函数指针和成员函数指针.
尝试2是我努力克服这个问题,但我现在得到了未解决的外部因素......
那么如何成功将我的Drawing()成员函数从另一个对象传递到另一个函数呢?
c++ member-function-pointers function-pointers member-functions
模板类的成员函数可以完全特化,例如
template<class A>
struct MyClass {
// Lots of other members
int foo();
};
template<class A>
MyClass<A>::foo() { return 42; }
template<>
MyClass<int>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)
会编译没有问题。请注意,这foo()不是模板函数,因此这与模板函数专业化无关(我可以理解在那里不允许部分专业化,因为它与重载相结合会变得非常混乱)。在我看来,上面的代码只是以下模板类专业化的简写:
template<class A>
struct MyClass {
// Lots of other members
int foo();
};
template<class A>
MyClass<A>::foo() { return 42; }
template<>
struct MyClass<int> {
// Copy all the other members from MyClass<A>
int foo();
};
template<>
MyClass<int>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)
这样对吗?
在这种情况下,我想知道为什么不允许使用类似速记的部分专业化,即为什么我不能写
template<class …Run Code Online (Sandbox Code Playgroud) c++ templates partial-specialization member-functions class-template
当尝试将全局函数作为成员函数(具有不同数量的参数)重载时,MSVC抛出错误C2660,该函数调用其正文中的全局函数.
这段代码:
void f(int* x, int y) { *x += y; }
struct A
{
int* x;
inline void f(int y)
{
f(x, y); // tries to call A::f instead of f
}
void u(void)
{
f(5);
}
};
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
error C2660: 'A::f' : function does not take 2 arguments
Run Code Online (Sandbox Code Playgroud) 我正在使用 TRI DDS - 这是我尝试调用的函数的原型:
template<typename T , typename Functor >
dds::sub::cond::ReadCondition::ReadCondition (
const dds::sub::DataReader< T > & reader,
const dds::sub::status::DataState & status,
const Functor & handler
)
Run Code Online (Sandbox Code Playgroud)
所以我有一个看起来有点像这样的类(省略了一些不相关的内容):
MyClass test{
public:
test(){... mp_reader = ...}; // not complete
start_reader()
{
dds::sub::cond::ReadCondition rc(*mp_reader,
dds::sub::status::DataState::any(),
do_stuff()); // This does not work
}
void do_stuff() {...}
private:
dds::sub::DataReader* mp_reader;
}
Run Code Online (Sandbox Code Playgroud)
所以我只是尝试传入函数 do_stuff() ..我知道这行不通,但我不确定要在这里放入什么来代替参数const & functor。我可以传入成员函数吗?- 如何指定类的实例?
我尝试将 lambda 放在那里并且它起作用了 - 但我无法访问 lambda 中的 mp_reader 因为它不在 lambda 的范围内。但无论如何,我真的不想使用 lambda,我真的想使用一个函数(因此,最终我可能能够传入一个外部函数)。
请参阅此处 …
我希望能够从类的对象中通过多个名称调用相同的成员函数。
例如:
#include <string>
#include <stdio.h>
class Log
{
public:
Log(std::string str)
: log(str)
{}
void print() const
{
puts(log.c_str());
}
const auto& output = print; // attempting to alias. does not work
private:
std::string log;
};
int main()
{
Log log("LOG: Log started.");
log.print();
log.output(); // both should call the same function.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码为我产生此错误(gcc 7.3.0)
main.cpp:15:15: error: non-static data member declared with placeholder ‘const auto’
const auto& output = print; // attempting to alias. does not work …Run Code Online (Sandbox Code Playgroud) 如何为成员函数正确调用 invoke_result?或者专门用于操作员成员函数。我试过std::invoke_result<T::operator[], size_type>没有成功。在这种情况下,正确的语法是什么?
考虑以下代码:
int main()
{
struct EmptyStruct{
void nonstatic_mf() const { std::cout <<"EmptyStruct\n"; }
};
EmptyStruct *esptr = nullptr;
esptr->nonstatic_mf();
}
Run Code Online (Sandbox Code Playgroud)
这是一个合法的 C++(它似乎在 gcc 和 clang 中工作)?
struct A
{
void f1()
{
f2(); // ok, though f2() is not declared before
}
void f2()
{}
void f3(X*) // error: unknown type name 'X'
{}
struct X
{};
};
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
为什么成员类型需要前向声明而成员函数不需要?
c++ ×10
member-functions ×10
class ×2
templates ×2
alias ×1
boost-bind ×1
c++17 ×1
function ×1
functor ×1
operators ×1
overloading ×1
pointers ×1
refactoring ×1
standards ×1
types ×1
windows ×1