Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

copy/assignment/move constructors and private data

4: Adsense

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 Tispe   Members   

1468
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 Ashaman73   Members   

13714
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 Bregma   Members   

9098
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 Álvaro   Members   

20927
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 Ryan_001   Prime Members   

3350
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.