• Advertisement
Sign in to follow this  

copy/assignment/move constructors and private data

This topic is 1370 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

 

Let's say we have class A and class B that both have private data and are very different from each other.

 

We want to implement copy/assignment/move constructors for both classes such that A can be constructed with private data from B and vice versa.

 

Do we have to invoke the friend keyword in order for these constructors to get access to the private data?

Share this post


Link to post
Share on other sites
Advertisement


Do we have to invoke the friend keyword in order for these constructors to get access to the private data?

If you dont have any public interface (eg public getter) and the target class is not a specialisation of the source class (in this case you could copy the protected variables), only using friend comes to mind.

Share this post


Link to post
Share on other sites

I agree with Bregma. Why is it that A can access B's private data in ways that other parts of the code can't? Perhaps the interface of B is incomplete?

 

Also, you will get much better answers if instead of "A" and "B" you tell us what the actual names of the classes are, so we can get an idea of why one would need to know the details of the other. We might be able to propose a better design too.

Share this post


Link to post
Share on other sites

There are times when its necessary for a class to access the internals of another...  Its not necessarily a bad design decision.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement