Jump to content

  • Log In with Google      Sign In   
  • Create Account

copy/assignment/move constructors and private data


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
4 replies to this topic

#1   Members   -  Reputation: 1410

Like
0Likes
Like

Posted 24 April 2014 - 01:27 AM

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?



#2   Crossbones+   -  Reputation: 13521

Like
2Likes
Like

Posted 24 April 2014 - 02:18 AM


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.


Ashaman

 

Gnoblins: Website - Facebook - Twitter - Youtube - Steam Greenlit - IndieDB - Gamedev Log


#3   Crossbones+   -  Reputation: 8042

Like
5Likes
Like

Posted 24 April 2014 - 05:17 AM

*
POPULAR

But... keep in mind the need to grab the private parts of another object is a code smell very indicative of poor design.  You might want to re-think the single purpose of your class(es) or their interface(s) before your project grows much more.


Stephen M. Webb
Professional Free Software Developer

#4   Crossbones+   -  Reputation: 19665

Like
0Likes
Like

Posted 24 April 2014 - 07:22 AM

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.



#5   Prime Members   -  Reputation: 2877

Like
0Likes
Like

Posted 24 April 2014 - 11:00 PM

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






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS