-6

I have 3 classes

class A {
    B *b;
}

class B {
C *c
}

class C {
....
}

What is the best way to access instance of class C inside class B from class A? Please also suggest a way to write the constructor for each of these classes?

Shyam
  • 37
  • 1
  • 8
  • 1
    Rethink what you want here. Do you want them to be private, or do you want other classes to touch them directly? The 2 are contradictory. If you want these members to be public though, just write the classes in the opposite order. – BoBTFish Aug 23 '17 at 10:17
  • 1
    If member are private, you should not access them. make them public or create public accessors. – Jarod42 Aug 23 '17 at 10:17
  • 1
    given the information you provide, the least overcomplicated solution is to make the member `public`. For anything else you need to consider what `A`,`B` and `C` really is, how they are related and why `c` was declared `private` in the first place – 463035818_is_not_an_ai Aug 23 '17 at 10:19
  • The easiest and perhaps the fastest way is to use the 'friend' keyword. – Marcel Kirchhoff Aug 23 '17 at 11:51
  • Look, this can be done, as others have mentioned, with 'friend', or a function giving you a pointer to a private member, or some such. But if you have to do this, you need to rethink your class design. This is not a good idea -- if it were, there would be no need for private members! In other words, you can do this, but you shouldn't want to :-) – Basya Aug 23 '17 at 12:31

1 Answers1

4

You have multiple possibilities to access members of other classes.

If a member is private (as in your case) you can either declare other classes as a friend of this class or write getters and setters.

Method 1: Getters and Setters

class A {
private: 
  int a;
public:
  int GetA() { return a; }
  void SetA(int newA) { a = newA; }
};

This however will grant access to your private data from every other place in your code. Though you can leave setA away causing only read access to the private a.


Edit:

As @tobi303 has correctly pointed out: Returning a raw pointer via a getter (as it would be in your case) is probably not a good idea. So I would recommend in this case to either return the object the pointer points to or use smart pointers like std::shared_ptr and return them:

class {
private:
  int *a;
  std::shared_ptr<int> b;
public:
  int getA() { return a ? *a : 0; } // only dereference the pointer if it points somewhere
  std::shared_ptr<const int> getB() { return b; } // return as std::shared_ptr<const int> to prevent indirect write access to a
};

Method 2: Friend Classes

class B
{};

class A {
private: 
  int a;
public: 
  friend class B;
};

In this case only B and A can access the private data of A. You can also grant access to the private data to only a few functions.

However, granting write access to private members often indicates a flaw in your design. Maybe they should be public (see Method 3) or you don't even need write access and just reading them would be completely sufficient for your needs.

Method 3: Public Members

class A {
public:
  int a;
};

Similar to Method 1 (with both the getter and the setter) you are now granting read and write access to As member a.

If you really need read and write access to a private member, Method 2 is the way to go. But as I've said: Think about your design once again, do you really need this?

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • imho your example doesnt fit perfectly. Returning an `int` via a getter is almost never a problem, however returning a raw pointer (that actually was supposed to be private) is rather questionable – 463035818_is_not_an_ai Aug 23 '17 at 11:05
  • @tobi303 Right, haven't realised that the OP uses pointers in his example. I provided an example using pointers for Method 1. – muXXmit2X Aug 23 '17 at 11:23
  • 1
    @Shyam Ask a new question rather than editing your question and making it completely different. – muXXmit2X Aug 25 '17 at 05:38