473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.
Dec 12 '07 #1
4 1561
gw****@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. 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
Dec 12 '07 #2
On Dec 12, 10:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 13 '07 #3
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
Dec 13 '07 #4
On Dec 13, 2:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bruce W...1 | last post by:
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP....
0
by: Daniel Lidström | last post by:
Hi, I've been trying a long time now to generate some XML using MC++ and XmlSerializer. I have a piece of C# code that produces exactly what I want, but I simply can't get the MC++ code to write...
2
by: John Carson | last post by:
One routinely hears that the order of initialisation of objects with static storage duration in different translation units cannot in general be guaranteed. I have a question about one way to...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
0
by: michael | last post by:
How can I reference an instance of a static class member accross all translation units? I don't want to pass a reference between dlls. Lib1.dll PROJECT 1 A.h class A { public:
4
by: James Aguilar | last post by:
Guys, When I specialize a template class member function (I.e. a member function of a template class) based on that class' type, bad things happen. Here's some code: ---- test_header.h...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.