Author Topic: C++ friend function generation & import  (Read 3549 times)

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
C++ friend function generation & import
« on: June 13, 2007, 06:29:54 am »
I have a C++ class that basically looks like this (header file):
Code: [Select]
class CFoo {
public:
   // ...
   friend void bar();
};

And the CFoo implementation file contains the implementation of the friend function bar as well:
Code: [Select]
void bar(){
   // do something
}

I've got two problems with respect to EA here:

1. On import, EA ignores the existing implementation of bar(), although I have switched on synchronization of implementation files.

2. If I define a class within EA under the same premises, and create the method implementation for bar() within the 'Initial Code' section, this implementation gets created under the name CFoo::bar()!

Apparently EA does not recognize that calling a method 'friend' within a class also removes it from class scope. This is probably also the reason why it doesn't recognize the already existing implementation: since the class scope qualifier is missing EA thinks it is another function!

Obviously the fix would be to have the code generation templates take the 'friend' stereotype into account: when generating method bodies, the class scope qualifier should be omitted for 'friend' methods.

But if I fix these templates, will this also affect import? I am asking this because it appears EA ignores global functions on principle - and these functions *will* in syntax and effect be global!

Any ideas?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8063
  • Karma: +118/-20
    • View Profile
Re: C++ friend function generation & import
« Reply #1 on: June 13, 2007, 02:37:28 pm »
My interpretation is that a friend function doesn't actually belong to a class, it's more of a forward declaration that this function exists somewhere and is allowed to access my private members.

As such, I think that no implementation should be generated at all for friend functions.  (And I have recorded this to be fixed)

sl@sh

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: C++ friend function generation & import
« Reply #2 on: June 14, 2007, 01:09:07 am »
I agree - especially if said friend function is actually a method within another class, it will already have a body within that class.

However, fixing this would also destroy my workaround for defining global operators within the model :( (see http://www.sparxsystems.com.au/cgi-bin/yabb/YaBB.pl?board=UMLPRO;action=display;num=1181029452 ) So, if the generation of friend function implementation gets suppresed in a future release, could you please add an option to stick with the current behaviour? Preferrably at function level? Or maybe you can think of sth else - I rather like my idea of defining a container for global functions by defining those operators 'friend' to the container...