jCoder
jCoder

Reputation: 66

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. The drawing action is being done by a big Renderer class. So there is a data transform between ObjectParameters class and MainDrawing class. I use to declare a public function to make possible the call from MainDrawing. But it can also be called by its user, and make the class object insecure.

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 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 */
};

Upvotes: 1

Views: 2398

Answers (3)

f180362 Asad Ullah
f180362 Asad Ullah

Reputation: 49

what I got is that you want that inherited class cannot access the parent class variable. For that you can simply do is make the variable private and function public. and inherit class as protect so that only child class can access and use the function. if you want code I can also help with that. First method is by declaring it protected and inherit the 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

friend class MainDrawing and make funcation Set_ZIndex() private so only friend class can access it

Upvotes: 0

m88
m88

Reputation: 1988

With the friend keyword: 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
}

Upvotes: 2

catnip
catnip

Reputation: 25388

You could make the private function a private member of an embedded class, like this:

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

Upvotes: 2

Related Questions