简体   繁体   English

我应该如何在包含的类对象中访问容器类的功能

[英]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 . 这是通过传递*thisclsDog的构造函数的初始列表中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.

相关问题 在类组合中,使包含的类可以访问容器类变量 - In class composition make possible for contained class to access container class variables 如何在“ boost :: function”中访问类实例(对象)指针? - How can I access class instance(object) pointer in “boost::function”? 如何从 function 访问 class object - How to access class object from a function 我应该如何在容器类中允许整数? - How may integers should I allow in my container class? 如果稍后定义返回对象的类,我应该如何从转换函数返回一个对象? - How should i return an object from a conversion function if the class of the returned object is defined later? 我应该创建一个 Class object 吗? - Should I create a Class object? 从包含的类调用容器类中定义的回调函数的推荐方法是什么? - What’s the recommended way to invoke callback function defined in container class from contained class? 访问作为指向该类的指针的向量的类的方法 - Access a method of a class contained as a vector of pointers to that class 对象如何访问包含它们的类的属性? - How can objects access attributes of the class in which they are contained? 我可以通过对象访问类中的函数中的变量吗? - Can I access a variable in a function which is in a class through an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM