How can I modify the protected values of a variable of a base class in a derived class function?

10k views Asked by At
class abc{
protected:
    int x; 
};
class b: public abc{
public :
    void something(abc a){ a.x = 1;}
};

I get an error in the second last line stating that I cannot access member x of variable a.

Error: Protected Member "abc::x" is not accessible through a "abc" point or object.

Is there another way to modify the x-value of variable a?

4

There are 4 answers

0
Steephen On

You can do simply as follows in derived class

void something(){ x = 1;}

Demo: http://coliru.stacked-crooked.com/a/b0f6153abfe6b783

0
Gerard van Helden On

Within something() you are in object scope, so you have a pointer to the current object with the this keyword. In C++, however, the default resolution within object scope, is the object's own scope, so you don't need to prefix it at all. Simply referring the property's name is enough to have the resolution succeed.

1
Ediac On

When inheriting, b can modify the values of the base class "it" inherited, but the base abc protected class attributes of other objects are still protected and reserved for themselves. In this case the abc a object you pass through the function something is another object not associated with an object b.

Access modifiers work on class level, meaning within an object b the private attributes of another object b can be accessed. However, this inheritance shown is basically saying that "b is an abc, but abc isn't necessarily b". In the function void something(abc a){ a.x = 1;}, if you change it to something(b a) instead, it would work, because a b can access another b.

However, remember that if:

int main(){
    abc data;
    abc data2;

    data.x = data2.x; //This won't work becuase you are outside the class
}

What you can do is declaring class b a friend of class abc, so that any object b can access the protected members of any other object abc.

Example:

class abc{
protected:
    int x; 

friend class b;
};
0
TheSmartWon On

One way to do what you are asking is to have a function setX() in class ABC like such:

class abc
{
    public:
        void setX(int x){ this->x = x; }
    protected:
        int x; 
};

Then declare something inside of class B like this:

class b: public abc
{
    public :
        void something(abc a){ a.setX(1); }
};

At that point though you might as well not have class B at all, but rather from the scope you are calling b.something(myABCObj) just call myABCObj.setX(1) instead.

The point of 'protected' is that class b has access to 'x' in class abc not through the parameter you are passing it but rather inside of itself.

For example, your class b could look like this:

class b: public abc{
    public :
        void something(){ x = 1; } //HA- I inherited a value from class abc that wasn't public and changed it!
};

So in a different scope you could create object b myB; and myB.x won't be allowed but myB will have access to 'x' inside of itself from its parent class.