Majd Soubh
Majd Soubh

Reputation: 19

C++ access protected data in Super Class

hello guys can anyone help me :

#include <iostream>
using namespace std;

class A
{
private:
    int a;

protected:
    int b;
    void set_value(int y, int u, int i)
    {
        a = y;
        b = u;
        c = i;
    }

public:
    int c;
};

class B : private A
{
public:
    A::set_value;       //Compilation Error !
    A::b;               //Compilation Error !
};

int main()
{
    B b;
    b.set_value(1,1,1);   //Compilation Error !
    cout<<b.b;            //Compilation Error !
}

How i Could Make protected member in super class can accessed directly (with out get() or set()) in main function using subclass

Upvotes: 0

Views: 62

Answers (2)

Majd Soubh
Majd Soubh

Reputation: 19

first when i use Clang++ it shows me error when i compile, When I use G++ compiler version 9.3.0 it works with me correctly Thanks Guys.

Upvotes: 0

florestan
florestan

Reputation: 4655

Quoting from cppreference (emphasis mine)

Using-declaration introduces a member of a base class into the derived class definition, such as to expose a protected member of base as public member of derived.

So the solution is to write the class as follows:

class B : private A
{
public:
    using A::set_value;       
    using A::b;               
};

Full example here.

It seems the access declarations were deprecated in C++98 and finally removed in C++11.

Upvotes: 2

Related Questions