gpt4 book ai didi

C++ 指向使用模板的非静态成员函数的指针

转载 作者:行者123 更新时间:2023-11-28 06:47:15 31 4
gpt4 key购买 nike

我在让成员函数指针工作时遇到问题(我不知道我正在尝试做的事情是否可行)。我想设置一个成员变量(它是指向另一个类中的非静态函数的指针),然后调用该函数。然后我希望能够将它设置为不同派生类中的另一个成员函数并调用它。 2 个派生类由模板创建,如下所示。我可以实现这一目标吗?如果可以,我做错了什么?

// Forward declare these, their implementation is irrelevant to the question 
class A;
class B;

// declare a base class that contains a function log() that we want to call
class BaseClass {
public:
BaseClass() {}
virtual ~BaseClass() {}

virtual void log() {} // want to call this via function pointers
};

//Using a template means we don't have to specify the type in the vector
template<class T>
class TemplateVectorOfBaseClass : public BaseClass {
protected:
std::vector<T> peripherals;
}

// this specific implementation does stuff using Class A
template <int randomTemplateParameter>
class DerivedTemplateClassThatDoesStuffWithAs : public BaseClass
public:
DerivedTemplateClassThatDoesStuffWithAs() : TemplateVectorOfBaseClass<A>() {}
void log() {do_something_here_involving_As();}
int i[randomTemplateParameter];
};

// this specific implementation does stuff using Class B
template <int randomTemplateParameter>
class DerivedTemplateClassThatDoesStuffWithBs : public BaseClass
public:
DerivedTemplateClassThatDoesStuffWithBs() : TemplateVectorOfBaseClass<B>() {}
void log() {do_something_here_involving_Bs();}
float f[randomTemplateParameter];
};

// Class that contains both templates as member variables
class ContainerClass {
public:
ContainerClass();

DerivedTemplateClassThatDoesStuffWithAs dtca<5>;
DerivedTemplateClassThatDoesStuffWithBs dtcb<10>;

void (BaseClass::*log)(); // pointer to member function log()
}

// usage
ContainerClass cc;

cc.container.log = &dtca.log;
cc.*log(); // should call vectorContainingAs.log()

cc.container.log = &dtcb.log;
cc.*log(); // should call vectorContainingBs.log()

最佳答案

我不能真正依赖你的代码,因为它充满了错误,所以这里是它应该如何使用简化版本:

class A { } ;
class B { } ;

class BaseClass {
public:
virtual void log () {
std::cout << "Log BaseClass." << std::endl ;
}
};

template <class T>
class TVector : public BaseClass { } ;

template <int N>
class DTCA : public TVector <A> {
public:
virtual void log () {
std::cout << "Log DTCA." << std::endl ;
}
} ;

template <int N>
class DTCB : public TVector <B> {
public:
virtual void log () {
std::cout << "Log DTCB." << std::endl ;
}
} ;

class Container {
public:
DTCA <5> dtca ;
DTDB <10> dtcb ;
void (BaseClass::*log) () ;
} ;

我假设前一部分的错误是由于错误的复制/粘贴造成的。您主要错误的部分是将方法分配给指针的方式,以及在对象上调用方法的方式:

int main () {
Container cc ;
cc.log = &BaseClass::log ; // Store the log member function into cc.log

(cc.dtca.*cc.log) () ; // Call it on dtca
(cc.dtcb.*cc.log) () ; // Call it on dtcb

return 0 ;
}

输出:

Log DTCA.

Log DTCB.

你不能从实例中获取方法的地址,你会得到类似这样的东西:

ISO C++ forbids taking the address of a bound member function to form a pointer to member function.

所以你把地址带到方法中,因为你的变量类型是 (BaseClass::*)() 你需要直接从 BaseClass 中获取它.然后,您需要使用 BaseClass 的实例或派生类来调用它。

关于C++ 指向使用模板的非静态成员函数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24743098/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com