Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access a Protected Variable Correctly?

I am trying to access one of the protected variable from items.h class using components.cpp class but I got the error confusing me: 😀

Item::Quantity’: cannot access forbidden protected member declared in class
‘Item’

item.h

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 protected:
        int32 Quantity;

component.h

#include "Items/Item.h"
Item* AddItem(class Item* Item, const int32 Quantity);

component.cpp

ItemAddResult Component::TryAddItem_Internal(class Item* Item)
{
    Items.Add(Item);
    return ItemAddResult::AddedAll(Item->Quantity);
}

>Solution :

You can solve this by making the class Component a friend of class Item by adding a friend declaration inside class Item as shown below:

item.h

class Item{
    friend class Component;
    protected: 
        int32 Quantity;
    //other members here
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading