开发者

What's the best way to access the internal data structure within a class?

开发者 https://www.devze.com 2023-04-12 05:22 出处:网络
I have a class A consistin开发者_开发知识库g of a bunch of internal data structures (e.g. m_data) and a few objects (e.g. ClassB):

I have a class A consistin开发者_开发知识库g of a bunch of internal data structures (e.g. m_data) and a few objects (e.g. ClassB):

class A
{
public:
      ...
private:
      int m_data[255];
      ClassB B[5];
}

What's the best way for B to access m_data? I don't want to pass m_data into B's function..

// updated: Many thanks for the responses. Let me provide more contextual info. I am working on an AI project, where I got some data (e.g. m_data[i]) at each time step. The class A needs to buffer these information (m_data) and uses a list of B's (example updated) to make inference. Class B itself is actually a base class, where different children derive from it for different purpose so I guess in this context, making B a subclass of A might not be clean (?)..


friend class ClassB;

Put this line anywhere in A's declaration if you want ClassB to access all of A's protected and private members.


One of:

  • Make ClassB a friend of A
  • Make A a sub-class of ClassB and make m_data protected rather than private

[In response to Mark B's comment]

If ever you feel the need to resort to a friend relationship, the design should be reconsidered - it may not be appropriate. Sub-classing may or may not make sense; you have to ask yourself "Is class A and kind of class ClassB?" If the question makes no sense intuitively, or the answer is just no, then it may be an inappropriate solution.


Ideally, you don't allow external access the data structure at all. You should rethink your approach, considering more the question "What are the functional requirements / use cases needed for ClassB to access instances of A" rather than offloading the management of the internal members to methods not managed within class A. You will find that restricting management of internal members to the class owning those members will yield cleaner code which is more easily debugged.

However, if for some reason this is not practical for your situation there are a couple possibilities that come to mind:

  • You can provide simple get/set accessor methods which, depending upon your requirements, can be used to access either a copy of or a reference to m_data. This has the disadvantage of allowing everybody access, but does so only through well defined interfaces (which can be monitored as needed).
  • ggPeti mentions use of friend, which may work for you, but it gives ClassB access to all of the internals of A.


A getData() function that returns m_data. Use setData() to change the value.

So in the function in class B you would create a pointer to the class type A variable that you created. Lets just call this pointer 'p'.

Just do p->getData(), p.getData() may be the answer. I think they do the same thing but c++ uses the '->' and some other languages use the '.'. Don't quote me on that one though.

Good luck, sir. Hope I helped ya.


What's the best way for B to access m_data?

Depends on the use.

This is how would I do it :

class ClassB
{
  // ...
  void foo( A &a )
  {
    // use a's data
  }
};
class A
{
  //...
  int m_data[255];
  ClassB & B;
};

Depending on the implementation, maybe ClassB is not needed at all. Maybe it's methods can be converted to functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号