简体   繁体   English

获取作文方法

[英]accessing methods of composition

This question may be somewhat vague, but bear with me. 这个问题可能有些模糊,但请耐心等待。

I have 3 classes in my project. 我的项目中有3个班级。 2 classes have a composition relationship with 1 class, that is, the 2 are part of the 1. 2个类与1个类具有组合关系,即2个类是1的一部分。

however I must have access to the 2 classes methods, but don't really want to make them totally public. 但是我必须能够访问2类方法,但实际上并不想让它们完全公开。

in the 1 class i have the 2 classes instantiated with private visibility, which prevents the 1 class object from accessing the 2 class methods 在1类中,我使用私有可见性实例化了2个类,这可以防止1类对象访问2类方法

This is in C++;) 这是在C ++中;)

EDIT: i'll put an example of what i have; 编辑:我将举例说明我所拥有的;

class Aclass
{
    private: int numA;
    public: void ExampleMethod();
};

class Bclass
{
    private: int numB;
    public: void ExampleMethodB();
};

class Cclass
{
    private: 
             Aclass Aobject;
             Bclass Bobject;
    public: 
};

void main()
{
    Cclass Cobject;

    Cobject.ExampleMethod();
}    

in the 1 class i have the 2 classes instantiated with private visibility, which prevents the 1 class object from accessing the 2 class methods 在1类中,我使用私有可见性实例化了2个类,这可以防止1类对象访问2类方法

Your assumption is wrong. 你的假设是错误的。 If the two objects are private , you can still access them from inside the class: 如果这两个对象是private ,您仍然可以从类中访问它们:

class A
{
public:
   foo();
};

class B
{
private:
   A a;
public:
   void goo() { a.foo(); } // this is ok, although a is private
                           // you can't access a from outside the class though
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM