Iburidu
Iburidu

Reputation: 460

C++ Class in class method inheritance

I have the following structure, and I would like to call foo from D. How is it possible? I got the error message, that I commented at the line below.

class A
{
protected:
    class B
    {
    public:
        B(x)
        {
            //...
        }

    protected:
        virtual void foo()
        {
            //...
        }
    };
};

class C : public A
{
protected:
    class D : public A::B
    {
    public:
        D(x) : B(x)
        {
            //empty
        }
    };

    void bar()
    {
        D var = D(x);
        var.foo(); //cant access protected member in class A::B
    }
};

Upvotes: 0

Views: 87

Answers (5)

Iburidu
Iburidu

Reputation: 460

Thanks for the answer. Making the foo function public is really a solution, but I really want foo only available from inherited class. The friend modifier is a thing, what many people hate, because it gives you access for things, thats you should not access.

But also you were right guys, I should access B::foo from D (from constructor is not an option is my case). I could create a function, which calls the original foo, so my solution is:

class A
{
protected:
    class B
    {
    public:
        B(x)
        {
            //...
        }

    protected:
        virtual void foo()
        {
            //...
        }
    };
};

class C : public A
{
protected:
    class D : public A::B
    {
    public:
        D(x) : B(x)
        {
            //empty
        }

        Dfoo()
        {
            B::foo();
        }
    };

    void bar()
    {
        D var = D(x);
        var.Dfoo();
    }
};

The solution was easy, but I did not know that a function (what is not static) can be called like this :)

Upvotes: 0

JSF
JSF

Reputation: 5321

You might want foo() to be public in D even though it is protected in A::B

If so you can add the following line after the public: line in the definition of D

using A::B::foo;

Upvotes: 0

Andrea
Andrea

Reputation: 16560

Actually you are trying to access from the class C a protected method of the class D ==> You can't access protected methods from the external of a class.

One solution could be to declare C as a friend class of D so C can access protected methods:

// ...

    class D : public A::B
    {
    public:
        D(x) : B(x)
        {
            //empty
        }

        // Declare C as friend of D
        friend class C;
    };

// ...

Adding the friendship declaration your code will compile and will work as expected.

Upvotes: 0

Das Louis
Das Louis

Reputation: 140

Try a friend class. You can make D a friend of B, and that should allow D to call foo().

Upvotes: 0

everettjf
everettjf

Reputation: 126

foo() is protected member function of B, means that foo() is only allowed to be called from its subclass( child class).

bar() is member function of C, and C is inheritance from A. So ,C is not subclass of B.

If you put bar in D, that's OK.As code below:

class A
{
protected:
    class B
    {
    public:
        B(int x)
        {
            //...
        }

    protected:
        virtual void foo()
        {
            //...
        }
    };
};

class C : public A
{
protected:
    class D : public A::B
    {
    public:
        D(int x) : B(x)
        {
            //empty
        }

        // Here is OK, while D is subclass of B
        void bar()
        {
            int x;
            D var = D(x);
            var.foo(); //cant access protected member in class A::B
        }
    };

    // void bar()
    // {
    //     int x;
    //     D var = D(x);
    //     var.foo(); //cant access protected member in class A::B
    // }
};


int main(){


    return 0;
}

Upvotes: 1

Related Questions