[英]How should I access a function of a container class in a contained class object
I have the following class structure 我有以下课程结构
class containingClass
{
int func1(int a);
containedClass containedClassObject;
}
I want to access func1
in containedClass
objects. 我想在containedClass
对象中访问func1
。 What will be the best way to achieve that? 实现这一目标的最佳方法是什么?
A naive solution that comes to mind is to pass a function pointer to the containedClass
's constructor, but then I get into a circular definition, as I need to pass a pointer to an object of the containingClass
as well. 我想到的一个天真的解决方案是通过一个函数指针containedClass
的构造,但后来我进入一个循环定义,因为我需要一个指针传递给一个对象containingClass
为好。
Any suggestions? 有什么建议么?
The containedClass
required a contract/API/function to be fulfilled by the int func1(int)
member of the containingClass
. 所述containedClass
所需的合同/ API /功能由满足int func1(int)
的成员containingClass
。 Unless the containedClass
explicitly requires access to an instance of the containingClass
for other purposes, the access can be provided via lambda (or std::bind
) and the containedClass
can have a std::function
with the correct signature member that holds the lambda. 除非containedClass
明确要求出于其他目的访问containingClass
的实例,否则可以通过lambda(或std::bind
)提供访问,并且containedClass
可以具有std::function
,并且具有正确的签名成员来保存lambda。
The only "trick" here would be to ensure that the lifetime of the objects are managed appropriately, ie the lifetime of the containingClass
instance is at least as long as required for use in the containedClassObject
object. 唯一的“绝招”这里将是确保对象的生命周期的适当管理,即寿命containingClass
实例只要至少需要在使用containedClassObject
对象。
A sample; 一个样品;
#include <functional>
class containedClass {
std::function<int(int)> functor_;
public:
void setter(std::function<int(int)> functor) { functor_ = functor; }
};
class containingClass
{
int func1(int a);
containedClass containedClassObject;
public:
containingClass()
{
containedClassObject.setter([this](int a) -> int { return this->func1(a); });
}
};
Even if this already over a year old, I would like to help other seraching people. 即使已经超过一岁了,我也想帮助其他的搜索族。
Here is another example using a reference to the containing class. 这是另一个使用对包含类的引用的示例。 Tested with mingw32-g++ 4.9.2 and -std=c++98. 已使用mingw32-g ++ 4.9.2和-std = c ++ 98进行测试。 Means it should work also with c++0x and c++11 意味着它也应该与c ++ 0x和c ++ 11一起工作
#include <string>
#include <iostream>
using namespace std;
class clsDog;
class clsEar{
public:
clsDog& myDog;
clsEar(clsDog &dog);
};
class clsDog{
public:
clsEar ear;
void pain(string fromPart){
cout << "dog has pain in his " << fromPart << endl;
}
clsDog():ear(*this){};
};
clsEar::clsEar(clsDog &dog): myDog(dog){
myDog.pain("ear");
}
int main(){
clsDog dog;
}
First class clsDog
makes it available for reference and pointers. class clsDog
使它可用于引用和指针。 NOT for actual values like non reference member variables. 不适用于非引用成员变量之类的实际值。
In class clsEar
a reference to clsDog
is created using clsDog& myDog
. 在class clsEar
到参考clsDog
使用创建clsDog& myDog
。 The constructor can set the reference pointer in the initializer list. 构造函数可以在初始化列表中设置引用指针。 It is important that the containing class clsDog
is passed by reference otherwise the compiler tell you that the class incomplete. 重要的是,包含类clsDog
必须通过引用传递,否则编译器会告诉您该类不完整。
Because clsEar
is now fully defined a normal member variable of clsEar
can be defined in clsDog
. 因为clsEar
现在完全定义的正常成员变量clsEar
可以在被定义clsDog
。 Because clsEar
's constructor needs a reference to clsDog
it must be passed to it's constructor. 由于clsEar
的构造函数需要对clsDog
的引用,因此必须将其传递给它的构造函数。 This is done in the initialisier list of clsDog
's constructor by passing *this
. 这是通过传递*this
在clsDog
的构造函数的初始列表中clsDog
的。
Last but not least the implementation of clsEar::clsEar
must be done. 最后但并非最不重要的clsEar::clsEar
必须完成clsEar::clsEar
实现。 It is necessary to do this after the complete definition of clsDog
to call the member functions or access member varibales of clsDog
in clsEar
. 在完全定义了clsDog
之后,有必要执行此操作,以调用clsDog
中的clsEar
的成员函数或访问成员变量。 Otherwise the compiler will tell you again, that the class clsDog
is of incomplete type. 否则,编译器会再次告诉您, clsDog
类的类型不完整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.