Search code examples
c++classinterfacefriend

Access private members of a class instantiated using interface


I have a class derived from an interface and a friend class of the derived class. I want to access the members of derived class which is instantiated as interface. It looks like this:

Interface:

class AInterface
{
public:
   virtual ~AInterface() = default;
   virtual void set(int a) = 0;
};

Derived class A with friend class B:

class B;
class A : public AInterface
{
public:
   ~A() override {}
   void set(int a) override
   {
      mem = a;
   }
private:
   friend class B;

   int mem = 0;
};

class B:

class B
{
public:
   B() 
   {
     a = new A();
     a->set(3);
   }

   int get_a()
   {
      // Access mem since it's a friend class
      return a->mem;
   }

private:
    AInterface *a;
}

main:

int main()
{
    B *b = new B();
    std::cout << b->get_a() << std::endl;
    return 0;
}

The program does not compile saying AInterface has no member named 'mem'. Do I need getter functions in the interface and implement it in A to achieve this or is there any other way to do it?


Solution

  • Now work

    #include <iostream>
    using namespace std;
    
    
    class AInterface
    {
    public:
        virtual ~AInterface() = default;
        int getMem() { return mem; }
        virtual void set(int a) = 0;
    protected:
        int mem = 0;
    };
    
    class A : public AInterface
    {
    public:
        ~A() override {}
        void set(int a) override
        {
            mem = a;
        }
    
    
    };
    
    class B
    {
    public:
        B()
        {
             a = new A{};
            a->set(3);
        }
    
        int get_a()
        {
            // Access mem since it's a friend class
            return a->getMem();
        }
    
    private:
        AInterface *a;
    };
    int main()
    {
        B *b = new B();
        std::cout << b->get_a() << std::endl;
        return 0;
    }
    
    1. Method which is override by child and is pure, should be virtual.
    2. If each class (child) Interface, variable int mem should be protected in interface. Now works fine, like you want.
    3. Add getter getMem()

    Version with friend

    #include <iostream>
    using namespace std;
    
    
    class AInterface
    {
    public:
        virtual ~AInterface() = default;
        virtual void set(int a) = 0;
    protected:
        friend class B;
        int mem = 0;
    };
    
    class A : public AInterface
    {
    public:
        ~A() override {}
        void set(int a) override
        {
            mem = a;
        }
    
    
    };
    
    class B
    {
    public:
        B()
        {
             a = new A{};
            a->set(3);
        }
    
        int get_a()
        {
            // Access mem since it's a friend class
            return a->mem;
        }
    
    private:
        AInterface *a;
    };
    int main()
    {
        B *b = new B();
        std::cout << b->get_a() << std::endl;
        return 0;
    }