Book a Demo

Author Topic: How do I model friend classes (C++)?  (Read 7523 times)

EricP

  • EA User
  • **
  • Posts: 122
  • Karma: +0/-0
    • View Profile
How do I model friend classes (C++)?
« on: June 05, 2010, 02:38:23 am »
At the risk of raising an issue about whether one should use friends...

I do have a legitimate need to declare a friend class in another class.  Searching "help" on "friend" yields several indications that it is supported, e.g.:

Enterprise Architect supports round trip engineering of C++ ... where the following conventions are used

Stereotype = friend
Applies to = Operation
Corresponds to = the friend keyword

So it is apparently supported, but no indication how to use it.

I tried putting "friend class MyFriend" in one class and then reverse-engineering (sync'ing) back with the model, on a suggestion from Simon in response to another "How do I..." question to "try it and see how EA handles it".  In this case it made no visible change in the model, and when I re-generated code from the class, the friend was gone.
« Last Edit: June 05, 2010, 02:38:45 am by rkt-med »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: How do I model friend classes (C++)?
« Reply #1 on: June 07, 2010, 08:51:08 am »
Actually, EA does handle friend operations to some extent, but not friend classes.

Makulik

  • EA User
  • **
  • Posts: 400
  • Karma: +0/-0
    • View Profile
Re: How do I model friend classes (C++)?
« Reply #2 on: June 07, 2010, 06:49:21 pm »
hi eric,

simply don't use <<friend>> dependencies, this isn't a very good idea usually and you ain't gonna need it!
BTW the <<friend>> stereotype is marked as obsolete since UML 2.x .
If you need help, how to eliminate your <<friend>> dependency i can give you a small article that shows how to accomplish this (for C++ at least).

HTH
g.

EricP

  • EA User
  • **
  • Posts: 122
  • Karma: +0/-0
    • View Profile
Re: How do I model friend classes (C++)?
« Reply #3 on: June 07, 2010, 10:00:45 pm »
Quote
simply don't use <<friend>> dependencies, this isn't a very good idea usually and you ain't gonna need it!

Good morning, Makulik.

I was kinda hoping to avoid the philosophical debates.  Rarely does any good ever come from them.

<<friend>> is a tool in my toolbox, and just like any other tool, one uses it where appropriate and not where not.  I have used friends maybe three or four times in all the time I have been using C++ and in every case it was absolutely the best available solution to the problem at hand.

That article you mentioned would be helpful... please email it to [email protected].  Thanks!

Thomas_Arnbjerg

  • EA User
  • **
  • Posts: 80
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: How do I model friend classes (C++)?
« Reply #4 on: June 07, 2010, 10:40:13 pm »
I simply assign a tagged value to the class where the friend should be declared. Hereafter the code generation template 'Class Body' is modified by adding the following line:

%classTag:"friend"=="" ? "": "\tfriend class " value ";"%

(btw: I'm using ACE where singletons are declared using friend functions combined with private constructors...)

Makulik

  • EA User
  • **
  • Posts: 400
  • Karma: +0/-0
    • View Profile
Re: How do I model friend classes (C++)?
« Reply #5 on: June 07, 2010, 11:26:50 pm »
Hi Eric,

I think since <<friend>> is marked as obsolete in UML and strongly discouraged in almost any coding style guide, as far I've seen them, i think it can't be called a philosophical debate anymore.
but anyway, i'll send you the article.

WBR
Günther