Can different member functions of the same class be in differenttranslation units?



 DEVELOP > c-Plus-Plus > Can different member functions of the same class be in differenttranslation units?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 12 Dec 2007 03:38:53 PM
Object: Can different member functions of the same class be in differenttranslation units?
If a class has more than one member function, is it possible for the
code for one to be in one translation unit (ie file) and the code for
another to be in a different translation unit? I would have thought
so, yet I still have a nagging doubt about it. If it is allowed, is it
a good idea?
My problem is that I have a class which does far, far too much, but I
haven't managed to break its functions down into several self-
contained groups. I thought that perhaps separating up the source
code, even if it is still part of the same class, might help by
clarifying what links between the various functions are required. Or
is there a better way?
Thanks in advance.
Paul.
.

User: "Victor Bazarov"

Title: Re: Can different member functions of the same class be in different translation units? 12 Dec 2007 03:47:50 PM
wrote:

If a class has more than one member function, is it possible for the
code for one to be in one translation unit (ie file) and the code for
another to be in a different translation unit?

Yes, absolutely. From the language point of view there is nothing
that should prevent it.

I would have thought
so, yet I still have a nagging doubt about it. If it is allowed, is it
a good idea?

That is up to you and your team. The only reason to split code on
translation units is to work on those separately. If you want to
allow one member of your team to work on one part of the class while
another works on some other part and no conflict exists from your
source control system, splitting might be a good idea.

My problem is that I have a class which does far, far too much, but I
haven't managed to break its functions down into several self-
contained groups.

You may need to break the class into several and inherit them or
instantiate them separately. Consider redesigning.

I thought that perhaps separating up the source
code, even if it is still part of the same class, might help by
clarifying what links between the various functions are required. Or
is there a better way?

Different areas of functionality belong to different classes, at
least according to my understanding of OOD.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "James Kanze"

Title: Re: Can different member functions of the same class be in differenttranslation units? 13 Dec 2007 05:57:25 AM
On Dec 12, 10:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

gw7...@aol.com wrote:

If a class has more than one member function, is it possible
for the code for one to be in one translation unit (ie file)
and the code for another to be in a different translation
unit?

Yes, absolutely. From the language point of view there is
nothing that should prevent it.

I would have thought so, yet I still have a nagging doubt
about it. If it is allowed, is it a good idea?

That is up to you and your team. The only reason to split
code on translation units is to work on those separately.

Most linkers still treat the object file as the unit of
granularity, which means that in a well written general purpose
class, which will be used in a variety of applications, every
non-virtual function will be in a separate source file, so that
the final executable only contains what it needs. (There are
exceptions, of course.)
Writing such libraries is a special case, however, and doesn't
concern most people.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "Victor Bazarov"

Title: Re: Can different member functions of the same class be in different translation units? 13 Dec 2007 07:29:35 AM
James Kanze wrote:

[..]
Most linkers still treat the object file as the unit of
granularity, which means that in a well written general purpose
class, which will be used in a variety of applications, every
non-virtual function will be in a separate source file, so that
the final executable only contains what it needs. (There are
exceptions, of course.)
[..]

I know that some advanced linkers manage to remove unused static
functions from the final program as part of optimizing it. This
is to complement your response, not to contradict it, mind you.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "James Kanze"

Title: Re: Can different member functions of the same class be in differenttranslation units? 14 Dec 2007 02:53:13 AM
On Dec 13, 2:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

James Kanze wrote:

[..]
Most linkers still treat the object file as the unit of
granularity, which means that in a well written general purpose
class, which will be used in a variety of applications, every
non-virtual function will be in a separate source file, so that
the final executable only contains what it needs. (There are
exceptions, of course.)
[..]

I know that some advanced linkers manage to remove unused static
functions from the final program as part of optimizing it. This
is to complement your response, not to contradict it, mind you.

I've heard this as well. The linkers on my principal target
don't, however, and if you're maintaining separate sources for
each function on one platform, it's easier to do it everywhere
than to use two different strategies.
It is more work. For low level classes with lots of more or
less similarly sized functions, it's worth it. But it's not
worth doing everywhere---for example, well over 90% of the code
of my RegularExpression class in the parser, which is invoked by
the constructor. Given that, it's probably not worth breaking
the other functions out into separate files---any user will pick
up 90% of the code anyway. (On the other hand, the parser
itself is complicated enough that it needs to be spread over
several source files. And because I originally developped it on
a 16 bit machine where every byte counted, my current
implementation still does have every function in a separate
file.)
If the class is only going to be used in one application, of
course, there's no point in it. If the function isn't used,
don't even write it, and if it is, it has to be linked in
anyway, so you might as well put it in the same file as the
other functions.
Also, if virtual functions are involved, the compiler will want
to put their address in the vtbl, whether they're invoked or
not. Which means that they will be linked in, so you might as
well put them all in the same source file.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER