我前段时间遇到过这个问题而且我放弃了,但最近又回来了.
#include <iostream>
class element2D;
class node2D
{
public:
void (element2D::*FunctionPtr)();
void otherMethod()
{ std::cout << "hello" << std::endl;
((this)->*(this->FunctionPtr))(); //ERROR<-------------------
}
};
class element2D
{
public:
node2D myNode;
void doSomething(){ std::cout << "do something" << std::endl; }
};
int main()
{
element2D myElement;
myElement.myNode.FunctionPtr = &element2D::doSomething; //OK
((myElement).*(myElement.myNode.FunctionPtr))(); //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在标记线上收到错误:
pointer to member type 'void (element2D::)()' incompatible with object type 'node2D'
Run Code Online (Sandbox Code Playgroud)
我真的很感谢你的帮助.今天有类似的问题部分帮助了我:链接.但它似乎并不是我的问题的完整答案.
实际上这两个问题只有一个区别 - 调用函数的点.
谢谢你的时间
如何让test.calculate中的函数指针赋值(可能还有其余的)工作?
#include <iostream>
class test {
int a;
int b;
int add (){
return a + b;
}
int multiply (){
return a*b;
}
public:
int calculate (char operatr, int operand1, int operand2){
int (*opPtr)() = NULL;
a = operand1;
b = operand2;
if (operatr == '+')
opPtr = this.*add;
if (operatr == '*')
opPtr = this.*multiply;
return opPtr();
}
};
int main(){
test t;
std::cout << t.calculate ('+', 2, 3);
}
Run Code Online (Sandbox Code Playgroud) 我需要使用一个成员函数指针,它接受在其他代码中使用的基类的参数.好吧,我只想做[某事]如下面的例子.这段代码工作正常,但我想知道这样的演员是否总是安全的?我不能在这里做dynamic或static投.
#include <cstdio>
class C
{
public:
C () : c('c') {}
virtual ~C() {}
const char c;
};
class D : public C
{
public:
D () : d('d') {}
virtual ~D() {}
const char d;
};
class A
{
public:
A () {}
virtual ~A() {}
void f( C& c ) { printf("%c\n",c.c); }
void g( D& d ) { printf("%c %c\n",d.c,d.d); }
};
int main (int argc, char const* argv[])
{
void (A::*pf)( …Run Code Online (Sandbox Code Playgroud) c++ pointers casting member-function-pointers reinterpret-cast
以下代码产生编译时错误:
'
base::print':无法访问类中声明的私有成员'base_der'
但是,我已public在派生类中创建了该成员.为什么这不起作用?
#include <iostream>
using namespace std;
class base
{
public:
int i;
void print(int i)
{
printf("base i\n");
}
};
class base_der : private base
{
public:
using base::print;
};
int main()
{
// This works:
base_der cls;
cls.print(10);
// This doesn't:
void (base_der::* print)(int);
print = &base_der::print; // Compile error here
}
Run Code Online (Sandbox Code Playgroud) c++ member-function-pointers member-pointers implicit-conversion private-inheritance
我想存储一个指向对象的指针和一个指向它的已知签名方法的指针.如果我知道该类,那么这个指针有类型:
int (MyClass::*pt2Member)(float, char, char)
Run Code Online (Sandbox Code Playgroud)
但是,如果我不知道类型,我怎么能存储指针?
我想做这样的事情:
myObject.callThisFuncLater(&otherObject, &otherObject::method)
Run Code Online (Sandbox Code Playgroud)
我怎么能存放指向方法method在myObject后来又打电话了吗?
c++ pointers member-function-pointers function-pointers callback
对我来说,将a转换void(Derived::*)()为a 看起来非常安全void(Base::*)(),就像在这段代码中一样:
#include <iostream>
#include <typeinfo>
using namespace std;
struct Base{
void(Base::*any_method)();
void call_it(){
(this->*any_method)();
}
};
struct Derived: public Base{
void a_method(){
cout<<"method!"<<endl;
}
};
int main(){
Base& a=*new Derived;
a.any_method=&Derived::a_method;
a.call_it();
}
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨演员a.any_method=&Derived::a_method;.这是一个阻止细微编程错误的障碍,还是一些让编译器编写者生活更轻松的东西?是否有变通方法让Base类具有指向Derived 无类型知识的成员函数的指针(也就是说,我不能Base使用模板参数创建模板Derived).
以下代码获取指向该函数的指针hello并将其打印出来:
package main
import "fmt"
type x struct {}
func (self *x) hello2(a int) {}
func hello(a int) {}
func main() {
f1 := hello
fmt.Printf("%+v\n", f1)
// f2 := hello2
// fmt.Printf("%+v\n", f2)
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我取消评论底部的部分,编译错误,说:
> ./junk.go:14: undefined: hello2
Run Code Online (Sandbox Code Playgroud)
所以我尝试过:
i := &x{}
f2 := &i.hello2
fmt.Printf("%+v\n", f2)
Run Code Online (Sandbox Code Playgroud)
...但错误:
> ./junk.go:15: method i.hello2 is not an expression, must be called
Run Code Online (Sandbox Code Playgroud)
好吧,也许我必须直接参考原始类型:
f2 := x.hello2
fmt.Printf("%+v\n", f2)
Run Code Online (Sandbox Code Playgroud)
不:
> ./junk.go:14: invalid method expression x.hello2 (needs pointer receiver: (*x).hello2) …Run Code Online (Sandbox Code Playgroud) 我正在使用带有类的c ++ 11线程库,它工作正常.
我只需要对此代码进行解释,以便我理解正确.
我的class.h
class foo {
private:
std::thread t1;
void worker();
public:
void work();
};
Run Code Online (Sandbox Code Playgroud)
class.cpp
#include "class.h"
void foo::worker() {
std::cout << "worker..." << std::endl;
}
void foo::work() {
t1 = std::thread(&foo::worker, this);
t1.join();
}
Run Code Online (Sandbox Code Playgroud)
现在是main.cpp
#include "class.h"
int main(int argc, char **argv) {
foo bar;
bar.work();
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白的是调用线程的类函数.
我使用std::thread(&foo::work, this)和解释这个调用如下:第一个参数是指向函数的指针,但我不知道为什么我不能在没有该&foo::部分的情况下调用它.第二个参数是线程知道父进程的类本身?
我找不到这方面的解释.只有代码,我想了解它.谢谢!
我有一种情况,我想要一个成员函数指针指向虚拟函数,避免动态调度.见下文:
struct Base
{
virtual int Foo() { return -1; }
};
struct Derived : public Base
{
virtual int Foo() { return -2; }
};
int main()
{
Base *x = new Derived;
// Dynamic dispatch goes to most derived class' implementation
std::cout << x->Foo() << std::endl; // Outputs -2
// Or I can force calling of the base-class implementation:
std::cout << x->Base::Foo() << std::endl; // Outputs -1
// Through a Base function pointer, I also get dynamic dispatch …Run Code Online (Sandbox Code Playgroud) struct A
{
void f() {}
};
void f() {}
int main()
{
auto p1 = &f; // ok
auto p2 = f; // ok
auto p3 = &A::f; // ok
//
// error : call to non-static member function
// without an object argument
//
auto p4 = A::f; // Why not ok?
}
Run Code Online (Sandbox Code Playgroud)
为什么我必须使用address-of运算符来获取指向成员函数的指针?
c++ standards language-design member-function-pointers function-pointers