accessing private variables

If I have 2 classes, person and hair for example, is it possible for person to access the protected pointer variable color_ ? and what would that code syntax look like?

class person
{
public:
constructor
etc...
}

class hair
{
public:
etc...
protected:
ptr * color_;

friend class person;
}
May I suggest using friend sparingly as it increases coupling b/w classes, at times unnecessarily, when alternatives like getters and setters exist. For instance, you can try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <memory>

//class Person;
class Hair
{
    public:
    Hair(std::shared_ptr<const std::string> color) : color_(color){};
        //copy-assignment deleted for std::unique_ptr
    std::shared_ptr<const std::string> getColor()const {return color_;};

    protected:
    std::shared_ptr<const std::string> color_ = nullptr;

    //friend class person;
};
class Person
{
    public:
    void show_color(const Hair& h)const;
};

int main()
{
    std::shared_ptr<const std::string> hair_color (new const std::string("blond"));
    Hair h(hair_color);
    Person p;
    p.show_color(h);
}

void Person::show_color(const Hair& h)const
{
    std::cout << *h.getColor() << '\n';
}
Yes I know I could rewrite the header file but in this particular situation I am not allowed to change the header file.

I am trying to implement a static member function that will be used in implementation of public methods, where the variable I am trying to access is inside another class's protected data (but is listed as a friend).

I have tried to use dot operator(.) to access it but it errors. something like
(maybe this is a bad example, supposing HairColor() is in class Person)

void Person::HairColor(const string)
{
blonde = hair.color;
}
Last edited on
Why Hair should only be a different class and not also an attribute of Person (edit: i.e. Person contains an object type Hair), I'm not sure but I suppose you have your reasons

I am trying to access is inside another class's protected data (but is listed as a friend)

You don't need friendship for that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <memory>

//class Person;
class Hair
{
    public:
    Hair(std::shared_ptr<const std::string> color) : color_(color){};
        //copy-assignment deleted for std::unique_ptr
    std::shared_ptr<const std::string> getColor()const {return color_;};

    protected:
    std::shared_ptr<const std::string> color_ = nullptr;

    //friend class person;
};
class Person
{
    public:
    std::string hair_color(const Hair& h)const;
};

int main()
{
    std::shared_ptr<const std::string> hair_color (new const std::string("blond"));
    Hair h(hair_color);
    Person p;
    std::cout << p.hair_color(h);
}

std::string Person::hair_color(const Hair& h)const
{
    return *h.getColor();
}

Last edited on
Topic archived. No new replies allowed.