473,398 Members | 2,389 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,398 software developers and data experts.

Spreading a class across several compilation units

Hello,

this more a question about good practice than it is about the C++
language itself, but still... my question is this:

I have a class representing the main window of my application which is
really growing big. Well, most of the action is happening in the main
window in my app so that's okay I guess. To solve the clutter, I thought
about splitting up the class into several smaller ones, but that didn't
feel right, mainly because it would have gone along with an increase of
complexity (further interaction would be needed between several classes)
which just isn't worth it.

Now my other idea was to group functions which share certain semantics
(e.g. signal handlers) into separate compilation units. However, this
would mean breaking up the class and spreading it over several files. I
had a look at the Nautilus source code, and that's how they do it and I
think it's a really good approach, but it's written in C afterall, so
I'm not sure if that applies to C++ as well.

Is that bad practice in C++? What would you do instead?

Regards,
Matthias Kaeppler
Sep 26 '05 #1
3 1572
Matthias Kaeppler wrote:
this more a question about good practice than it is about the C++
language itself, but still... my question is this:

I have a class representing the main window of my application which is
really growing big. Well, most of the action is happening in the main
window in my app so that's okay I guess. To solve the clutter, I thought
about splitting up the class into several smaller ones, but that didn't
feel right, mainly because it would have gone along with an increase of
complexity (further interaction would be needed between several classes)
which just isn't worth it.

Now my other idea was to group functions which share certain semantics
(e.g. signal handlers) into separate compilation units. However, this
would mean breaking up the class and spreading it over several files. I
had a look at the Nautilus source code, and that's how they do it and I
think it's a really good approach, but it's written in C afterall, so
I'm not sure if that applies to C++ as well.

Is that bad practice in C++? What would you do instead?


I think it's a fair practice. If there were an option of making several
classes, I'd prefer that, but since there isn't (or so you claim), putting
a class in more than one translation unit is just fine. If neither were
an option, I'd probably put them in more than one file, and then created
a file with a bunch of #include for all those sources. Then you'd have
only one translation unit, although it sometimes creates unreasonable
stress for the compiler/optimizer. Recently I had to downgrade the 'O'
flag for a compiler that was unable to handle translation units each
about 130-140K.

Bottomline: do split them.

V
Sep 26 '05 #2
Matthias Kaeppler wrote:
Hello,

this more a question about good practice than it is about the C++
language itself, but still... my question is this:

I have a class representing the main window of my application which is
really growing big. Well, most of the action is happening in the main
window in my app so that's okay I guess. To solve the clutter, I thought
about splitting up the class into several smaller ones, but that didn't
feel right, mainly because it would have gone along with an increase of
complexity (further interaction would be needed between several classes)
which just isn't worth it.

Now my other idea was to group functions which share certain semantics
(e.g. signal handlers) into separate compilation units. However, this
would mean breaking up the class and spreading it over several files. I
had a look at the Nautilus source code, and that's how they do it and I
think it's a really good approach, but it's written in C afterall, so
I'm not sure if that applies to C++ as well.

Is that bad practice in C++? What would you do instead?

Regards,
Matthias Kaeppler


Splitting an implementation of a class between two files should not
cause any problems. Nor would I say that doing so per se is considered
bad practice, though it is somewhat unusual.

It's far more likely someone would question why the implementation of a
single class is so large to begin with. After all, the more code that
is piled into a class, the less of a "class" it becomes. Classes should
provide encapsulation, coherency, and focus to a program's code. Those
benefits seem unlikely to be attained from a class that needs more than
one source file just to contain it.

I would guess that a good portion of the code in the class could be
moved outside of it. Only code that needs to execute as a class method
should remain as part of the class. Doing so actually improves
encapsulation. There's an article on this very topic by Scott Meyers
that may be relevant. I found it online, although registration may be
required in order to read the entire article:

http://www.cuj.com/documents/s=8042/cuj0002meyers/

Greg

Sep 26 '05 #3
Greg wrote:
It's far more likely someone would question why the implementation of a
single class is so large to begin with. After all, the more code that
is piled into a class, the less of a "class" it becomes. You mean like Java does? Sorry, couldn't resist :D

No seriously, you're right of course. I also thought about this issue,
but I feel that these methods (which are for a good part signal
handlers, just tons of them) belong semantically to the class, that's
why I didn't want to split the class into smaller ones. I mean, I
/could/ introduce some sort of controller class, which would take care
of handling all the signals, but it would need to modify private members
of the MainWindow class, and thus would have to be friend'ed, thereby
breaking encapsulation even more (most signal handlers modify the
behavior of the window in some form, so most of them need to access
private data).
I would guess that a good portion of the code in the class could be
moved outside of it. Only code that needs to execute as a class method
should remain as part of the class. Doing so actually improves
encapsulation. There's an article on this very topic by Scott Meyers
that may be relevant. I found it online, although registration may be
required in order to read the entire article:

http://www.cuj.com/documents/s=8042/cuj0002meyers/


Hm, too bad you have to sign up to read it, sounds interesting. Well, I
could read enough however to understand where Meyers is going. Applying
this view to my problem, I don't think that all these signal handlers
should be made global functions (for some of the reasons mentioned in
the article snippet, such as the need for overriding base class methods).
I would also have to vastly expand the class' interface at the public
level, because the global signal handlers would have to modify the
window's state.

I don't think that's a serious option in my particular case, though it
was quite insightful to read about it. I think, along with Victor's
comment, I'll stick to my plan. Thank you both.

Regards,
Matthias
Sep 26 '05 #4

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

Similar topics

8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
11
by: Michael Gaab | last post by:
Compilation in c generally has four phases 1. Preprocessing 2. Compilation 3. Assembly 4. Linking. If I use a flag that will not link the code, order of compilation is not an issue,...
10
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and...
6
by: Plamen Doykov | last post by:
Hi all I have converted a simple project from ASP.NET 1 to 2.0 with the latest prerelease of Visual Studio 2005. The problem is I can't access internal members from the code behind. It gives:...
22
by: dean | last post by:
Hi all, We have a few existing classes that we want to put into either another class or a namespace, so that we can avoid naming conflicts. As far as I can see the syntax is identical from a...
14
by: Glen Dayton | last post by:
While looking at some old code I ran across a snippet that increments a pointer to access different members of a structure: .... struct Badness { std::string m_begin; std::string m_param1;...
17
by: Jason Doucette | last post by:
I am converting a C-style unit into a C++ class. I have an implementation function that was defined in the .cpp file (so it was hidden from the interface that exists in the .h file). It uses a...
8
by: =?ISO-8859-1?Q?Florian_B=FCrzle?= | last post by:
Hi! I've organized my code in different files - the declarations of global variables and methods are contained in a file global.hh (for compilation via makefile under Linux). In this file I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.