gpt4 book ai didi

c++ - 是否可以将派生函数成员的指针保存在基类使用的另一个类中

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:40 26 4
gpt4 key购买 nike

基本上我有一个类,比如说 Parameter,它有一个 get 和 set 变量。

我还有一个基类,比方说 Vehicle,它有一个方法 registerParameter(...),它接受一个指向函数成员的指针作为 getter 和一个指向函数的指针成员作为二传手。然后,该方法应该将这两个指针写入参数类的对象并将该对象放入 vector 中。

最后但同样重要的是,我们有一个派生类,比方说 Car,我们用字符串 "color" 调用 registerParameter(...) code> 作为参数名称和来自该派生类的 getter 和 setter。

代码示例:

参数文件

#ifndef BASE_H
#define BASE_H
#include "base.h"

class Parameter
{
std::string (Base::*get)();
void (Base::*set)(std::string);
};

#endif

基础文件

#ifndef PARAMETER_H
#define PARAMETER_H
#include <vector>
#include "parameter.h"

class Base
{
public:
std::vector<Parameter> list;
void registerNew(std::string (Base::*get)(), void (Base::*set)(std::string))
{
Parameters parameter;
parameter.get = get;
parameter.set = set;
list.push_back(parameter);
}
};

#endif

派生文件

class Derived
{
public:
Derived derived()
{
registerNew(&getColor, &setColor);
}

std::string getColor()
{
return this->color;
}

std::string setColor(std::string newColor)
{
this->color = newColor;
}
private:
std::string color;
};

我已经考虑这个问题好几天了,直到周五晚上我才真正需要解决方案。

最佳答案

你不能做正在尝试的事情:

类型std::string (Base::*)()std::string (Derived::*)() 非常不同。 std::string (Derived::*)() 不能自动转换为 std::string (Base::*)()

采用以下场景。

struct Base
{
int foo() { return 10; }
};

struct Derived : Base
{
int bar() { return 20; }
};

int main()
{
Base base;

int (Base::*bf)() = &Base::foo;
(base.*bf)(); // Should be able to call Base:foo(). No problem.

bf = &Derived::bar; // This is a compiler error. However, if this were allowed....
(base.*bf)(); // Call Derived::bar()?? That will be a problem. base is not an
// instance of Derived.
}

更新

你可以这样做:

#include <string>
#include <vector>

class Base;

// Create a base class Functor that provides the interface to be used by
// Base.
struct Functor
{
virtual ~Functor() {}
virtual std::string get(Base& base) = 0;
virtual void set(Base& base, std::string) = 0;
};

// Create a class template that implements the Functor interface.
template <typename Derived> struct FunctorTemplate : public Functor
{
// typedefs for get and set functions to be used by this class.
typedef std::string (Derived::*GetFunction)();
typedef void (Derived::*SetFunction)(std::string);

// The constructor that uses the get and set functions of the derived
// class to do itw work.
FunctorTemplate(GetFunction get, SetFunction set) : get_(get), set_(set) {}

virtual ~FunctorTemplate() {}

// Implement the get() function.
virtual std::string get(Base& base)
{
return (reinterpret_cast<Derived&>(base).*get_)();
}

// Implement the set() function.
virtual void set(Base& base, std::string s)
{
(reinterpret_cast<Derived&>(base).*set_)(s);
}

GetFunction get_;
SetFunction set_;
};

class Base
{
public:
std::vector<Functor*> functorList;

void registerFunctor(Functor* functor)
{
functorList.push_back(functor);
}
};

class Derived : public Base
{
public:
Derived()
{
// Register a FunctorTemplate.
registerFunctor(new FunctorTemplate<Derived>(&Derived::getColor,
&Derived::setColor));
}

std::string getColor()
{
return this->color;
}

void setColor(std::string newColor)
{
this->color = newColor;
}
private:
std::string color;
};

关于c++ - 是否可以将派生函数成员的指针保存在基类使用的另一个类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24546236/

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