mzimmers
mzimmers

Reputation: 887

C++ exposing member of private object

In such an instance:

class A
{
private:
    B b;
};

class B
{
private:
    C c;
};

class C
{
public:
    void func();
};

Is there a way to make func() visible to A, or do I have to use an intermediate call in B?

Upvotes: 0

Views: 67

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17464

No, because c is private in B, so instances of A cannot access it.

You will have to add an appropriate accessor to B.

This could forward the func call, like void B::func() { return c.func(); }.

Or, it could expose the c directly, like C& B::getC() { return c; } then b.getC().func().

Only you can decide what B should "let" other classes do with its stuff.

Upvotes: 1

Related Questions