在我的班上,如果我想指一个班里的人,
struct S
{
static int get();
int do_something();
int x;
};
我知道,
int (*p)() = S::get;
不幸的是,这并不适用于非静态成员
int (*p)() = S::do_something; // error
既然静态成员函数是一个普通函数,那么从下面的引语来看,一个非静态成员函数也是一个普通函数,那么为什么它不能工作呢?什么意思?
(9.2/10)注意:非静态成员函数的类型是普通函数类型,而非静态数据成员的类型是普通对象类型。没有特殊的成员函数类型或数
如果我有一个具有纯虚函数的基类,以及一个实现该函数的派生类,那么打印出该函数实际被调用的地址的最简单方法是什么?
class A { public: virtual void func()=0; }
class B:A {
public:
void func() { /*implementation*/ }
void func2() { *** I WANT TO PRINT THE ADDRESS OF func() HERE! *** }
};
另外,打印类中静态函数的地址和打印全局函数的地址最简单的方法是什么?
我正在编写一个Gameboy仿真器,根据CPU的指示,我在这里使用这个结构(在cpp.hpp中)来存储有关它们的信息。该地图用于通过与其个人操作码相等的键访问所有这些信息:
struct instruction {
std::string name; //name of the instruction
int cycles; //clock cycles required to be executed
int paramNum; //number of params accepted
我试图从成员函数进行一些回调,一切都很好,直到我尝试使用一个从2个类派生的模板类作为回调对象时,我得到了以下错误:
error C2440: 'reinterpret_cast' : Pointers to members have different representations; cannot cast between them
这件事告诉我成员函数指针有不同的表示(doh!)
这些表示是什么?它们之间的区别是什么?
考虑两个类
class A{
public:
int i;
A(){}
explicit A(const int ii):i(ii){}
virtual ~A(){
cout<<"~A - "<< i <<endl;
}
virtual void inc(){
i++;
cout<<"i: "<<i<<endl;
}
};
class B: public A{
public:
i
我正在尝试将指向成员函数的指针存储在一个结构中,该结构将用于稍后在我的程序中调用该函数。
如下所示:
// abc.h
namespace XYZ {
typedef void func(const uint8_t *buf, int len);
struct holder
{
// other members
func * storePtr;
}
} // end of namespace
另一个文件为:
// pqr.h
#include abc.h
namespace XYZ {
class pqr {
// data members and other func
所以基本上我正在解决著名的“哲学家就餐”问题,5个哲学家正在使用克隆生成。关键是我希望每个哲学家都有一个id (从0到4)。我计划使用克隆传递参数来实现。下面是代码(我省略了一些子函数)
void philoshopher(void* arg)
{
int i = &arg;
while (TRUE)
{
printf("Philosopher %d is thinking", i);
take_forks(i);
printf("Philosopher %d is eating"
我在尝试调用函数时遇到下面的C2064错误。我做错了什么?
//error C2064: term does not evaluate to a function taking 1 arguments
#include <iostream>
#include <string>
using namespace std;
class DXStartupEncoder {
public:
void EncodeA(string& message) const { message = "A"; }
void EncodeB(string&
我有一个std::function对象的向量,定义如下:
std::vector<std::function<void()>> funcs = { myFunc1, myFunc2, myFunc3 };
//The functions are defined like this:
//void myFunc1() { ... }
我试图在这个数组中搜索一个特定的函数。我的第一次尝试是使用std::find函数,如下所示:
auto iter = std::find(funcs.begin(), funcs.end(), myFunc2);
//CS2679: bina
在我的名为Mat的类中,我希望有一个接受另一个函数作为参数的函数。现在我有下面的4个函数,但是在调用print()时我得到了一个错误。第二行给了我一个错误,但我不明白为什么,因为第一行可以工作。唯一的区别是函数f不是Mat类的成员,而f2是。失败的是:error: no matching function for call to Mat::test( < unresolved overloaded function type>, int)'
template <typename F>
int Mat::test(F f, int v){
return f
当我想在符号(::).It总是有一个尾随空格后获得宏中函数的地址时,就会产生这个问题,它会报告一个错误。
我在Mojave i386中使用了G++。
例如,我有一个类:
struct A{
void b();
};
还有一个宏:
#define ACC(name) &A::name
我用它来获取A::b的指针
void(*accab)() = ACC(b);
我会得到这样的错误:
error: cannot initialize a variable of type 'void (*)()' with an rvalue of type 'void (A::
幸运的是,我正在编写代码,并犯了这样的错误:
int f ()
{
. . .
}
int g;
int main()
{
. . .
if (f) {
// this is actually true
}
}
稍后,我检查和构造如下:
f;
g;
甚至不会犯任何的错误。这似乎是合乎逻辑的,但这样做的目的是什么?如何使用呢?检查是否存在某种标识符?强制编译器不要优化函数或变量?
UPD:刚刚检查和行为是相同的使用VS和minGW。