简体   繁体   English

有什么方法可以使 class function 只能从另一个 class ZC1C425268E68385D1AB5074F 调用

[英]Is there any way to make a class function only callable from another class function?

I'm developing a little 2D "renderer" which draws things with parameters read from classes on screen.我正在开发一个小的 2D“渲染器”,它使用从屏幕上的类读取的参数来绘制事物。 The drawing action is being done by a big Renderer class.绘图动作由大型渲染器 class 完成。 So there is a data transform between ObjectParameters class and MainDrawing class.因此 ObjectParameters class 和 MainDrawing class 之间存在数据转换。 I use to declare a public function to make possible the call from MainDrawing.我用来声明一个公共 function 以使来自 MainDrawing 的调用成为可能。 But it can also be called by its user, and make the class object insecure .但它也可以由其用户调用,并使 class object不安全

So is there any way to make the declared class function be only callable (however the way is public, private or protected) by another class?那么有什么方法可以使声明的 class function只能由另一个 class 调用(但是方法是公共的、私有的或受保护的)?

class ObjectParameters {
public:
    COORD position;
    int Width;
    int Height;
    COLORREF elemColor;
private:
    int _zIndex;
public:
    ObjectParameters();
    ~ObjectParameters();

    /* This line is the code which won't be called by the user, 
    /* but MainDrawing class needs it for setting the layers.
    /* I don't want to make it callable from user, 
    /* because it can occur errors. */
    void Set_ZIndex(int newValue);

};

class MainDrawing {
public:
    MainDrawing();
    ~MainDrawing();
    
    /* Here will change the object's z-index to order the draw sequence,
    /* so it calls the Set_ZIndex() function */
    void AddThingsToDraw(ObjectParameters& object);
private:
    /* OTHER CODES */
};

With the friend keyword: https://en.cppreference.com/w/cpp/language/friend使用friend关键字: https://en.cppreference.com/w/cpp/language/friend

// Forward declaration. So that A knows B exists, even though it's no been defined yet.
struct B;

struct A {
    protected: 
    void foo() {}

    friend B;
};

struct B {
    void bar(A& a) {
        a.foo();
    }
};

int main()
{
    A a; B b;
    b.bar(a);

    //a.foo(); Not allowed
}

You could make the private function a private member of an embedded class, like this:您可以将私有 function 设为嵌入式 class 的私有成员,如下所示:

class MyOuterClass
{
public:
    class MyInnerClass
    {
private:
        void MyPrivateFunction () {}
    public:
        void MyPublicFuncton ()
        {
            MyPrivateFunction ();
        }
    };
};

what I got is that you want that inherited class cannot access the parent class variable.我得到的是您希望继承的 class 无法访问父 class 变量。 For that you can simply do is make the variable private and function public.为此,您只需将变量设为私有并将 function 设为公开即可。 and inherit class as protect so that only child class can access and use the function.并继承 class 作为保护,以便只有子 class 可以访问和使用 function。 if you want code I can also help with that.如果您想要代码,我也可以提供帮助。 First method is by declaring it protected and inherit the class.第一种方法是声明它受保护并继承 class。

    class ObjectParameters {
    
        public:
    int Width;
    
    int Height;
    
        private:
    int _zIndex

;

    public:
   

     ObjectParameters();
~ObjectParameters();

    /* This line is the code which won't be called by the user,
    /* but MainDrawing class needs it for setting the layers.
    /* I don't want to make it callable from user,
    /* because it can occur errors. */
    protected:
   

     void Set_ZIndex(int newValue);

    };

    class MainDrawing:ObjectParameters {

    public:
MainDrawing();

~MainDrawing();

Set_ZIndex(5);
    /* Here will change the object's z-index to order the draw sequence,
    /* so it calls the Set_ZIndex() function */
    private:
    /* OTHER CODES */
    };

second method is declare the MainDrawing as friend in ObjectParameters第二种方法是在 ObjectParameters 中将 MainDrawing 声明为友元

friend class MainDrawing and make funcation Set_ZIndex() private so only friend class can access it friend class MainDrawing并将Set_ZIndex()私有,因此只有朋友 class 可以访问它

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

相关问题 如何使一个函数只能从另一个函数调用? - How do I make a function only callable from another function? 有没有办法使成员函数不能从构造函数调用? - Is there a way to make member function NOT callable from constructor? 从另一个类调用另一个类的函数 - Calling a function of another class from another class 从另一个类访问函数而没有任何关系 - Access function from another class without having any relations 有什么方法可以从基类函数参数中推断出模板参数吗? - Is there any way to deduce template parameters from base class function parameters? c++ 中是否有办法确保 class 成员 function 不会更改任何 ZA2F2ED4F8EBC2CBB4C21A2 成员? - Is there a way in c++ to make sure that class member function isnt changing any of the class data members? 为什么子类的私有成员函数可以从父类中调用? - Why is a private member function of a subclass callable from the parent class? 从另一个类中的一个类调用函数 - Calling a function from one class in another class Is there a way in C++ to restrict a function of a given class to another class only(without using inheritance, friend)? - Is there a way in C++ to restrict a function of a given class to another class only(without using inheritance, friend)? 我是否只能通过其他类成员函数访问成员函数? - Can I make a member function only accessible through another class member function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM